For static variables, static initialization blocks, variables, initialization blocks, constructors,
They are initialized sequentially (static variables, static initialization blocks) > (variables, initialization blocks) > constructors. We can also verify this with the following test code:
Public classInitialordertest {//Static Variables Public StaticString Staticfield ="Static Variables"; //variables PublicString field ="variables"; //Static initialization blocks Static{System. out. println (Staticfield); System. out. println ("Static initialization blocks"); } //Initialize block{System. out. println (field); System. out. println ("Initialize block"); } //Constructors Publicinitialordertest () {System. out. println ("Constructors"); } Public Static voidMain (string[] args) {Newinitialordertest (); }}Initialordertest.class
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:
classParent {//Static Variables Public StaticString P_staticfield ="Parent Class--Static variable"; //variables PublicString P_field ="Parent Class--Variable"; protected inti =9; protected intj =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 PublicParent () {System. out. println ("Parent Class--constructor"); System. out. println ("i="+ i +", j="+j); J= -; }}Parent.class
Public classSubclass extends Parent {//Static Variables Public StaticString S_staticfield ="Subclass--Static variables"; //variables PublicString S_field ="Subclass--Variables"; //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 PublicSubclass () {System. out. println ("sub-class--constructor"); System. out. println ("i="+ i +", j="+j); } //Program Entry Public Static voidMain (string[] args) {System. out. println ("sub-class Main method"); Newsubclass (); }}Subclass.class
Run the above code with the following results:
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=20
Now, 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 creating the 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 Java classes