Jvm and other loaders (1 ).
First, let's look at a sample program:
Package com. tfdd. test;/*** @ desc class load validation * @ author chenqm * @ date February 1, February 2, 2016 */class Singleton {private static Singleton singleton = new Singleton (); public static int count1; public static int count2 = 0; private Singleton () {count1 ++; count2 ++;} public static Singleton getInstance () {return singleton ;}} public class SingletonTest {public static void main (String [] args) {Singleton singleton = Singleton. getInstance (); System. out. println (singleton. count1); System. out. println (singleton. count2 );}}
Guess what the output result is? It is said that 80% of java programmers will make mistakes!
1
0
This is the result. Let's leave it alone. Next, let's talk about our class loader.
Class loading is roughly divided into three parts:: Load, connect, and initialize.
Load: Search for and load binary data of the class
Connection: 1. verify (ensure the accuracy of the loaded class) 2. prepare (allocate memory for static variables of the class and initialize it to the default value) 3. resolve (convert the symbolic reference in the class into a direct reference)
Initialization: Assign the correct initial value to the static variables of the class (that is, assign the given value)
Then let's look at how java programs use classes: active and passive. Note the following:
All JAVA Virtual Machine implementations must be initialized only when each class or interface is "active for the first time" by the JAVA program.
So what is active use? There are basically six situations:
-- Create a class instance
-- Access a static variable of a class or interface, or assign a value to the static variable
-- Call static methods of the class
-- Reflection
-- Initialize a subclass of A Class
-- Classes marked as startup classes during startup of Java virtual machines
After learning this knowledge, I will answer the previous questions now,
Singleton. getInstance (); the static method of the class is called, which is suitable for the first active use of this class! Then we enter the initialization stage.
Initialization is already part of step 3 of class loading. The default value has been assigned to the connection preparation section in step 2. So it should be a process like this:
1. singleton = null; count1 = 0; count1 = 0
2. singleton = new Singleton (); execute the constructor and the result is count0 = 1; count1 = 1
3. The count0 value is not skipped during initialization. The count1 value is 0, so the result is count0 = 1; count1 = 0.
(* Note) The assignment sequence of static variables of the class is executed in the order of code writing.
The modification code is as follows:
Package com. tfdd. test;/*** @ desc class load check * @ author chenqm * @ date February 1, February 2, 2016 */class Singleton {public static int count1; public static int count2 = 0; private static Singleton singleton = new Singleton (); private Singleton () {count1 ++; count2 ++ ;}public static Singleton getInstance () {return singleton ;}} public class SingletonTest {public static void main (String [] args) {Singleton singleton = Singleton. getInstance (); System. out. println (singleton. count1); System. out. println (singleton. count2 );}}
The output result is
1
1
It proves that the assignment order is executed in the order of code writing!