Java Virtual Machine class loading mechanism-Case Analysis
In the article "class loading mechanism of Java Virtual Machine", the course of class loading is elaborated in detail, and several examples are analyzed briefly. At the end of the article, I leave a suspense to you, here we will unveil this suspense. It is recommended that you read the "Java Virtual Machine class loading mechanism" and then read it again, which will be impressive. Otherwise, it will be okay ~~
The following is the program code:
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;}
Q: What is the output of this program?
This is a problem I have seen on the forum. I think it is a golden book.
This kind of knowledge is usually found in the minds of friends:
Assignment order in Java:
1. Assignment of static variables of the parent class
2. Assignment of its own static variables
3. assign values to the parent class member variables
4. Assignment of parent class Blocks
5. assign values to the parent Constructor
6. assign values to member variables
7. assign values to the block
8. Assignment of self-built Constructors
OK. What is output according to this theory? Answer output: 1 4. Is this correct? It must be incorrect. This is not to say that the above rule is incorrect, but that it cannot be simply applied.
The correct answer is:
23a=110,b=014
Isn't that incredible? One of the main points here is that instance initialization does not have to begin after class initialization.
The lifecycle of the class is: Load-> verify-> prepare-> parse-> initialize-> use-> uninstall, initialization and assignment of class variables are only involved in the preparation and initialization phases. Therefore, only the two phases are analyzed;
In the preparation stage of the class, you need to allocate memory for the class variable and set the default value. Therefore, the class variable st is null and B is 0. (Note that if the class variable is final, during compilation, javac will generate the ConstantValue attribute for the value. In the preparation phase, the VM will set the variable to the specified value according to the ConstantValue settings. If static is defined hereFinalInt B = 112, then the value of B in the preparation phase is 112, instead of 0 .)
The class initialization stage needs to be implemented as the class Constructor (the class constructor is used by the compiler to collect all the values of static statement blocks and class variables and combine them to generate class constructor in the order of statements in the source code, the object construction method is (), The class construction method is (), Which can be seen in the stack information). Therefore, first execute the assignment statement of the first static variable, that is, st = new StaticTest (), and then initialize the object, object initialization is to initialize the member variables before executing the constructor, so print 2-> set a to 110-> execute the constructor (print 3, at this time a has been assigned a value of 110, but B only sets the default value 0, but does not complete the assignment action). After the initialization of the object is complete, continue to execute the previous statements of the class constructor. I will not elaborate on it next, execute the statements in the source code in sequence.
It also involves a cold knowledge, that is, there is a special logic in nested initialization. In particular, the embedded variable happens to be a static member and is an instance of this class.
This can lead to an interesting phenomenon: "instance initialization actually exists before static initialization ".
In fact, there is no advance. You need to know the time when the java record is initialized or not.
Let's look at a simplified code and explain the key issues clearly:
public class Test { public static void main(String[] args) { func(); } static Test st = new Test(); static void func(){}}
?? Follow these steps:
When executing this code segment, the static Initialization is triggered by calling the main method. When initializing the static part of the Test class, the st member is encountered. However, this variable references the instance of this class. The problem arises. At this time, the static initialization process is not complete, and the instance must be initialized. Is that true? From the human perspective. However, from the java perspective, the static initialization process will not be re-triggered in the future no matter whether the static part is initialized or not. Therefore, when instantiating the st variable, the initialization of the instance is actually embedded in the static initialization process, and the initial position of static Initialization is embedded in the problem of the landlord. This causes the instance to initialize completely before static initialization. This is also the reason that a has a value of B and has no value. Finally, considering the text order, the results are obvious.
?? For details, I have a conclusion in my mind. If the loading mechanism of classes is vague, you can refer to the blog article recommended at the beginning ~ If you have any questions, please leave a message.