For static variables, static initialization blocks, variables, initialization blocks, constructors, theirinitialization order is (static variable, static initialization block) > (variable, initialization block) > constructor。 We can also verify this with the following test code:
public class Initialordertest {
static variables
Public static String Staticfield = "static variable";
Variable
Public String field = "Variable";
Static initialization blocks
Static {
System.out.println (Staticfield);
SYSTEM.OUT.PRINTLN ("Static initialization Block");
}
Initialize block
{
System.out.println (field);
System.out.println ("initialization block");
}
Constructors
Public Initialordertest () {
System.out.println ("constructor");
}
public static void Main (string[] args) {
New Initialordertest ();
}
}
Running the above code, we will get the following output result:
1. Static variables
2. Static initialization block
3. Variables
4. Initializing blocks
5. Constructors
This is in full conformity with the above mentioned. So what happens to the inheritance? We still get the final result with a 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 blocks
static {
System.out.println (P_staticfield);
System.out.println ("Parent class-static initialization block");
}
Initialize block
{
System.out.println (P_field);
System.out.println ("Parent class-Initialization block");
}//Constructors
Public Parent () {
System.out.println ("Parent class-constructor");
System.out.println ("i=" + i + ", j=" + j);
j =;
}
}
public class Subclass extends Parent {
static variables
public static String S_staticfield = "sub-class-Static variable";
Variable
Public String S_field = "Subclass--variable";
Static initialization blocks
static {
System.out.println (S_staticfield);
System.out.println ("Subclass-static initialization block");
}
Initialize block
{
System.out.println (S_field);
System.out.println ("Subclass-Initialization block");
}//Constructors
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 results are immediately present in front of us:
Parent Class--Static variable
Parent class--static initialization block
Subclass--Static variables
Subclass--Static initialization block
Sub-class Main method
Parent class--variable
Parent class--initialization block
Parent class--constructor
I=9, j=0
Subclass--Variables
Subclass--Initialization block
Sub-class--constructor
i=9,j=20Now, the results have been self-evident. The initialization of a static variable and a static initialization block for a subclass is done before the parent class's variables, initialization blocks, and constructors are initialized.static variables, static initialization blocks, variables, initialization blocks are initialized in order, depending on the order in which they appear in the class.
Perform process analysis
(1) Access Subclass.main (), (this is a static method), so the loader will look for the compiled subclass class code (that is, the Subclass.class file). During loading, the loader notices that it has a base class (that is, what extends means), and then it loads the base class. Whether you create a base class object or not, this process always happens. If the base class also has a base class, the second base class is also loaded, and so on.
(2) performs static initialization of the base class, then static initialization of the next derived class, and so on. This order is important because the static initialization of a derived class is likely to depend on the correct initialization of the base class member.
(3) when all the necessary classes have been loaded, start executing the main () method body and create an object with new subclass ().
(4) class subclass There is a parent class, call the constructor of the parent class, and you can use super to specify which constructor to call (that is, the first thing that the beetle () constructor does).
The construction and construction order of the base class is the same as for the derived class. First, the variables in the base class are initialized in literal order, and then the rest of the constructor of the base class is executed.
(5) The child class member data is initialized in the order in which they are declared, executing the remainder of the subclass constructor.
Initialization order of classes in Java