Source: Zhu Xiao
In the "Java Virtual machine class loading mechanism" in detail the process of class loading, and a few examples of a brief analysis, at the end of the article left a suspense to you, here to uncover this suspense. Recommended to read the "Java Virtual machine class loading mechanism" This article again to see this, the impression will be more profound, if not, there is no relationship ~ ~
Here is the program code:
| 123456789101112131415161718192021222324252627282930313233 |
package jvm.classload;public class StaticTest{ public static void main(String[] args) { staticFunction(); } static StaticTest st = new StaticTest(); static { System.out.println("1"); } { System.out.println("2"); } StaticTest() { System.out.println("3"); System.out.println("a="+a+",b="+b); } public static void staticFunction(){ System.out.println("4"); } int a=110; static int b =112;} |
The question is: what is the output of this program?
This is a question I see on the forum, I think compare the Golden code.
In general, for such questions, the knowledge in the minds of the Little friends:
Order of assignment in Java:
1. Assigning static variables to a parent class
2. Assigning a static variable to itself
3. Assigning a value to a parent class member variable
4. Parent Class block Assignment
5. Assigning values to parent constructors
6. Assigning values to self-member variables
7. Self-block assignment
8. Assigning a value to a constructor function
OK, what is the output according to this theory? Answer output: 1 4, so correct hmm? Certainly not correct, this is not to say that the above rules are not correct, but that can not be simple to apply the rule.
The correct answer is:
| 12345 |
2 3 a= 110 ,b= 0 1 4 |
Isn't it a little weird? And listen to my one by one, one of the main points here: instance initialization does not have to start initialization after class initialization is complete.
The life cycle of the class is: Unload with initialization----use------------------------------for the initialization and assignment of class variables only during the preparation phase and initialization phase, so only the two phases are analyzed;
The prep phase of a class needs to be done to allocate memory for class variables and set default values, so the class variable st is null, B is 0, and it is important to note that if the class variable is final, compile-time Javac will generate the Constantvalue property for value. In the prepare phase, the virtual machine sets the variable to the specified value according to the setting of Constantvalue, if this is defined here: Static final int b=112, then the value in prep Stage B is 112, not 0. )
The initialization phase of a class needs to do is to execute a class constructor (the class constructor is a compiler that collects all static statement blocks and assignment statements for class variables by merging the generated class constructors in the order of the statements in the source code, the object is constructed by <init> (), and the class is constructed by <clinit> (), Can be seen in the stack information), so the first static variable is executed first the assignment statement is St = new Statictest (), the object is initialized, the initialization of the object is to initialize the member variable and then execute the construction method, so the printing 2-> set A is 110-> The construction method is executed (printing 3, at which point A has been assigned a value of 110, but B is only set the default value of 0, does not complete the assignment action), and so on after the initialization of the object to continue to execute the previous class constructor statements, the next is not detailed, according to the statement in the source code in the order of execution.
It also involves a cold knowledge that there is a special logic in nesting initialization. In particular, the embedded variable is exactly a static member, and is an instance of this class.
This can lead to an interesting phenomenon: "instance initialization actually occurs before static initialization."
In fact, there is no advance, you need to know the timing of initialization of Java records.
Look at a simplified code that explains the key questions:
| 1234567 |
public class Test { public static void main(String[] args) { func(); } static Test st = new Test(); static void func(){}} |
According to the code above, there are the following steps:
- First, when the code is executed, the static initialization is triggered first by the call of the Main method.
- When initializing the static portion of the test class, you encounter this member of St.
- But coincidentally, this variable refers to an instance of this class.
- So the problem comes, when the static initialization process is not finished to initialize the instance part. Is that so?
- From the human point of view yes. But from the Java point of view, once you start initializing the static section, no matter whether it is completed or not, the static initialization process will no longer be triggered again.
- Therefore, when the ST variable is instantiated, it is actually embedding the instance initialization into the static initialization process, and in the landlord's problem, embedded in the static initialization of the starting position. This leads to the initialization of the instance completely before static initialization. This is the reason why A has value B is not worth.
- Finally, considering the text order, the result is obvious.
See here in detail, the heart probably has a conclusion, if the loading mechanism of the class is relatively vague, you can refer to the opening of the recommended blog-there are questions welcome message.
Java Virtual machine class loading mechanism--case study