Up-depth Java virtual machine- - deep Java Virtual Machine (ii)--Class loader details (above), in the previous article, we explained the life cycle of the class loading and linking, this one we went on to look down.
initialization of the class : initialization of a class begins after the class's life cycle has finished loading and joining. During the initialization phase of the class, theJava virtual machine executes the initialization statement of the class, assigning a value to the static variables of the class, in which the class is initialized in two ways: (1) Assigning a value at the declaration of the variable. (2) Assign a value at a static code block, such as the following code,a is the first initialization,B is the second type of initialization
[HTML]View Plaincopyprint?
- public class Test
- {
- public static int a = 0;
- public static int B;
- static{
- b=2;
- }
- }
public class Test{public static int a = 0;public static int b; static{b=2;}}
Both the declaration of static variables and the initialization of static code blocks can be considered as initialization of static variables, and the initialization of static variables of classes is sequential. Sequence for class files from top to bottom to initialize, think of this, think of a very shameless face test , share to everyone to see:
[Java]View Plaincopyprint?
- Package com.bzu.csh;
- Class Singleton
- {
- Private static Singleton Singleton = new Singleton ();
- Public static int counter1;
- Public static int counter2 = 0;
- Private Singleton ()
- {
- counter1++;
- counter2++;
- }
- Public static Singleton getinstance ()
- {
- return singleton;
- }
- }
- Public class Test
- {
- Public static void Main (string[] args)
- {
- Singleton Singleton = Singleton.getinstance ();
- System.out.println ("counter1 =" + Singleton.counter1);
- System.out.println ("counter2 =" + Singleton.counter2);
- }
- }
Package Com.bzu.csh;class singleton{private static Singleton Singleton = new Singleton ();p ublic static int Counter1;publi c static int counter2 = 0;private Singleton () {counter1++;counter2++;} public static Singleton getinstance () {return Singleton;}} public class Test{public static void Main (string[] args) {Singleton Singleton = singleton.getinstance (); System.out.println ("counter1 =" + Singleton.counter1); System.out.println ("Counter2 =" + Singleton.counter2);}}
Let's see what the program will output here.
Don't know what the answer is, if you don't mind, you can write your answer to the comment and see how many people have the same answer as you. Let me first talk about the answer I just started. I think it will output:
Counter1 = 1
Counter2 = 1
I don't know if everyone's answer is this, anyway, mine is. Let's take a look at the correct answer:
I don't know if you did it right, but I just started doing it wrong. OK, now let me explain why this is the answer. Before giving an explanation, let's start by looking at a concept:
The way Java programs use classes can be divided into two types
Active use
Passive use
• all Java Virtual machine implementations must initialize each class or interface when it is " first active " by a Java program
Active use (six types)
– Create an instance of the class
– Access a static variable for a class or interface, or assign a value to the static variable
– Call the static method of the class
– Reflections (e.g. Class.forName ("Com.bzu.csh.Test"))
– Initializes a subclass of a class
–java class (Java Test) that is identified as the startup class when the virtual machine is started
OK, we start to explain the above answer, the program starts running, first executes the main method, executes the first statement of the main method, invokes the static method of the Singleton class, The static method of calling the Singleton class here is to actively use the Singleton class. So start loading the singleton class. In the process of loading the Singleton class, the static variable is assigned the default value first .
Singleton=null
Counter1 = 0
Counter2 = 0
After assigning them the value of the default value, the only thing to do is initialize the static variable and initialize the variable that was already assigned at the time of the Declaration. As we mentioned above, initialization is assigned from top to bottom of the class file. So first assigns the value to the Singleton, assigns it the value, must carry on its construction method, then executes the counter1++;counter2++; So here's counter1 = 1;counter2 = 1; After executing this initialization, and then performing the initialization of the Counter2 , we declared that he was initialized to 0 , so the value of Counter2 changed to 0. Executes the output after the initialization is complete. So that's it, yes
Counter1 = 1
Counter2 = 0
Class initialization steps
(1) If a class has not been loaded or connected, load and connect the class first
(2) If the class has a direct parent class, and the parent class is not initialized, initialize the immediate parent class first.
(3) If there is an initialization statement in the class, execute these initialization statements directly in sequence
On top of us, we said that the Java Virtual Machine implementation must initialize them when each class or interface is " first Active " by the Java program , with six instructions for active use. In addition to the six scenarios described above, other ways of using Java classes are considered passive and do not cause initialization of classes. The "active use" of a subclass in a program causes the parent class to be initialized, but the "active" use of the parent class does not cause the subclass to initialize (it is impossible to say that an object of the class is generated and all subclasses in the system are initialized)
Note: calling the LoadClass method of the ClassLoader class loads a class, not the active use of the class, and does not cause the initialization of the class.
When a Java Virtual machine Initializes a class, all of its parent classes are required to be initialized, but this rule does not apply to the interface.
When a class is initialized, the interface it implements is not initialized first
When an interface is initialized, its parent interface is not initialized first
Therefore, a parent interface is not initialized because of its sub-interfaces or the initialization of the implementation class. The initialization of the interface is only caused when the program first uses a static variable for a particular interface. Active use of a class or interface can be considered only if the static variable or static method accessed by the program is indeed defined in the current class or in the current interface. The subclass is not initialized if it is the parent class property of the called Child class.
Deep Java Virtual Machine (iii)-Initialization of class's life cycle (bottom) class