Java program loading process

Source: Internet
Author: User

Yesterday's written test Alibaba had a very complicated question about the java program loading process. It took a long time to come back and study it. The original question code is as follows to determine the output:

Public class StaticTest {public static int k = 0; public static StaticTest s1 = new StaticTest ("s1"); public static StaticTest s2 = new StaticTest ("s2 "); public static int I = print ("I"); public static int n = 99; public int j = print ("j"); {print (" ");} static {print ("static block");} public static int print (String s) {System. out. println (++ k + ":" + s + "\ ti =" + I + "\ tn =" + n); ++ n; return ++ I ;} public StaticTest (String s) {System. out. println (++ k + ":" + s + "\ ti =" + I + "\ tn =" + n); ++ I; ++ n ;} public static void main (String [] args) {new StaticTest ("init ");}}

 

First, the code output is provided:
1: j I = 0 n = 0 2: constructed block I = 1 n = 1 3: s1 I = 2 n = 2 4: j I = 3 n = 3 5: construct block I = 4 n = 4 6: s2 I = 5 n = 5 7: I = 6 n = 6 8: static block I = 7 n = 99 9: j I = 8 n = 100 10: constructed block I = 9 n = 101 11: init I = 10 n = 102

 

I didn't expect that I only created an object and actually executed so many statements! Next we will analyze each output statement one by one. First, we need to have a rough understanding of the loading process of java programs: static code in the first execution class, including static member variable initialization and execution of static statement blocks; non-static code in the second execution class, including initialization of non-static member variables and execution of non-static statement blocks, and finally execution of constructors. In the case of inheritance, the static code of the parent class is executed first, and then the static code of the subclass is executed; then the non-static code and constructor of the parent class are executed; finally, execute the non-static code and constructor of the subclass. As shown in the figure below: the first statement prints j-related content, so 7th lines of code are executed. Obviously, this line of code executes non-static variable assignment operations, which does not seem to comply with the above java program loading rules. Execute the code according to the preceding rules. First, assign a value to the static variable k, and then create a static instance of the class. Then we will find that the first print statement may be related to the static instance object of this class. We try to create this static instance, and then the program loading process becomes the above standard loading process: first execute the static code, then the non-static, and finally the constructor. Because the execution of static code is performed in the order of code, only the first static variable k will assign values when creating the static instance, and neither the static variables nor the static statement block will exist; then run the non-static code. The first non-static code is the variable j value assigned in line 1 of the Code. This explains why the first print statement is a code of 7th rows. This also explains the second and third prints. The second print statement executes non-static code and then calls the constructor to create the Instance Object s1. Similarly, 4th to 6 print statements are executed when the static Instance Object s2 is created. After the creation of two static instance objects, the static variable I will be assigned a value, which is 7th print statements. The static member variable n will be assigned a value later. Then execute the static statement block and print 8th rows of statements. After the static code is executed, run the non-static code section, assign values to j according to the order before and after writing the code, and then execute the non-static statement block, this is the print statement for rows 9th and 10. After all the preceding steps are executed, execute the class constructor to create an object. This is the 11th-line print statement. Through the above analysis, we found that the execution sequence of the above Code still conforms to the java program loading process described at the beginning. It is only because there are two static instance variables of this class that complicate the print statement. In this case, the print process is similar to a recursion, and each recursion is executed according to the standard loading process. At the beginning, the Code gave a lot of questions. I felt that various exceptions would be thrown during the running process, but the code was magically printed with 11 statements, which was indeed surprising. The first question is whether the class contains a static object of the class itself, which will lead to loop recursion. You can try to remove the static code from line 3rd or line 4, and run the program, and a StackOverflowError error will be prompted. Why does static objects not cause stack overflow rather than static objects? This is because static member variables belong to all classes, and all class objects share the static member variables, that is, the static member variables only have one copy. Therefore, during recursion, when you find that the static variable is being created, the system will not create the variable again, so it will not recursion. However, if it is a non-static object, a new object will be created every time this new statement is encountered during recursion, resulting in stack overflow. Another question in this code is the initial value of variable I. The I value is printed in the static function print, but the variable I may not be defined when printing (I is not defined in the first six print statements ). The program passed, indicating that the declaration of static variables will be completed before initialization and the initial value 0 will be assigned.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.