When you go to the interview, you will often encounter such questions: give you two classes of code, they are inherited, each class only has the constructor method and some variables, the constructor may have a piece of code that performs some operation on the variable value, and some code that outputs the variable value to the console, so that we can judge the output result. This is actually a test of our understanding of the class initialization sequence in the case of inheritance.
We all know that for static variables, static initialization blocks, variables, initialization blocks, constructors, their initialization sequence is as follows (static variables, static initialization blocks)> (variable, initialization block)> constructor. We can also verify this through the following test code:
Java 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:
Static variables
Static initialization Block
Variable
Initialization Block
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:
Java code
Class Parent {
// Static variables
Public static String p_StaticField = "parent class -- static variable ";
// Variable
Public String p_Field = "parent class -- variable ";
// 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 ");
}
}
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 ");
}
// Program entry
Public static void main (String [] args ){
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
Parent class -- Variable
Parent class -- initialize the block
Parent class -- Constructor
Subclass -- Variable
Subclass -- initialize the block
Subclass -- Constructor
Now the results are self-explanatory. You may notice that the initialization of the subclass is not completed after the parent class is fully initialized, in fact, the initialization of static variables and static initialization blocks of sub-classes is completed before the initialization of the parent class variables, initialization blocks, and constructors.
So what is the sequence between static variables and static initialization blocks, and between variables and initialization blocks? Are static variables always initialized before the static initialization block, and variables always initialized before the initialization block? In fact, this depends on the order in which they appear in the class. Take static variables and static initialization blocks as examples.
Similarly, we should write a class for testing:
Java code
Public class TestOrder {
// Static variables
Public static TestA a = new TestA ();
// Static initialization Block
Static {
System. out. println ("static initialization block ");
}
// Static variables
Public static TestB B = new TestB ();
Public static void main (String [] args ){
New TestOrder ();
}
}
Class TestA {
Public TestA (){
System. out. println ("Test -- ");
}
}
Class TestB {
Public TestB (){
System. out. println ("Test -- B ");
}
}
Run the above Code and you will get the following results:
Test --
Static initialization Block
Test -- B
You can change the front and back positions of variables a, B, and static initialization blocks at will, and you will find that the output results change with the order they appear in the class, this indicates that static variables and static initialization blocks are initialized according to their defined sequence in the class. Similarly, variables and initialization blocks follow this rule.
After learning about the class initialization sequence in the case of inheritance, you can easily determine the final output result.