Java class initialization sequence
For static variables, static initialization blocks, variables, initialization blocks, constructors, their initialization sequence is (static variables, static initialization blocks)> (variables, initialization blocks)> constructor. We can also verify this through the following test code:
Public class InitialOrderTest {
// Static variables
Public static String staticField = static variable;
// Variable
Public String field = variable;
// Static initialization Block
Static {
System. out. println (staticField );
System. out. println (static initialization block );
}
// Initialize the block
{
System. out. println (field );
System. out. println (initialization block );
}
// Constructor
Public InitialOrderTest (){
System. out. println (constructor );
}
Public static void main (String [] args ){
New InitialOrderTest ();
}
}
Run the above code and we will get the following output:
1. Static variables
2. Static initialization Block
3. Variables
4. initialize the block
5. Constructor
This is exactly the same as what we mentioned above. What will happen with inheritance? We still get the final result using a piece of test code:
Class Parent {
// Static variables
Public static String p_StaticField = parent class -- static variable;
// Variable
Public String p_Field = parent class -- variable;
Protected int I = 9;
Protected int j = 0;
// Static initialization Block
Static {
System. out. println (p_StaticField );
System. out. println (parent class -- Static initialization block );
}
// Initialize the block
{
System. out. println (p_Field );
System. out. println (parent class -- initialization block );
} // Constructor
Public Parent (){
System. out. println (parent class -- constructor );
System. out. println (I = + I +, j = + j );
J = 20;
}
}
Public class SubClass extends Parent {
// Static variables
Public static String s_StaticField = subclass -- static variable;
// Variable
Public String s_Field = subclass -- variable;
// Static initialization Block
Static {
System. out. println (s_StaticField );
System. out. println (subclass -- Static initialization block );
}
// Initialize the block
{
System. out. println (s_Field );
System. out. println (subclass -- initialization block );
} // Constructor
Public SubClass (){
System. out. println (subclass -- constructor );
System. out. println (I = + I +, j = + j );
} // Program entry
Public static void main (String [] args ){
System. out. println (subclass main method );
New SubClass ();
}
}
Run the above Code and the result is immediately displayed in front of our eyes:
Parent class-static variables
Parent class-static initialization Block
Subclass-static variable
Subclass-static initialization Block
Subclass main method
Parent class -- Variable
Parent class -- initialize the block
Parent class -- Constructor
I = 9, j = 0
Subclass -- Variable
Subclass -- initialize the block
Subclass -- Constructor
I = 9, j = 20 now, the results are self-explanatory. The initialization of static variables and static initialization blocks of subclass is completed before the initialization of the parent class variables, initialization blocks, and constructors. Static variables and static initialization blocks. The initialization sequence of variables and initialization blocks depends on the sequence in which they appear in the class.
Execution Process Analysis
(1) Access SubClass. main (), (this is a static method), so the loader will find the compiled SubClass class code (that is, the SubClass. class file) for you ). During the loading process, the loader notices that it has a base class (that is, the meaning of extends), so it loads the base class. Whether you create a base class object or not, this process will always happen. If the base class has a base class, the second base class will also be loaded, and so on.
(2) execute static initialization of the base class, and then initialize the static initialization of the next derived class, and so on. This order is very important because the "static initialization" of the derived class may depend on the correct initialization of the base class members.