Java Initialization Order(transferred) 1 The class is loaded first when you start an instance of new B. (The class is only loaded by the Java class loader when it is created with the new call) 2, when the class is loaded, the parent class A is loaded first, and then the subclass B is loaded.
3, after loading the parent class A, complete the static action (including static code and variables, their levels are the same, initialization occurs in the installation code)
4, after loading sub-Class B, complete the static action
Class mount complete, start instantiation
1, when instantiating subclass B, first instantiate the parent class A
2, the member instantiation (non-static code) when the parent class A is instantiated
3, the construction method of parent Class A
4, member instantiation of subclass B (non-static code)
5, the construction method of sub-class B
initializes the static code of the parent class---> initializes the static code of the subclass--initializes the non-static code of the parent class---> initializes the parent class constructor---> Initialize subclass non-static code---> Initialize Subclass constructors
Test code:
Abstract class Base
{
public int Age=getnumber (100);
static{
System.out.println ("Base static block");
}
{
System.out.println ("Base nonstatic block");
}
static int Sage=getnumber (50);
Base () {
System.out.println (age);
System.out.println ("base start");
Draw ();//Will call the subclass override method, here is 0!
System.out.println ("base End");
}
static int getnumber (int base) {
SYSTEM.OUT.PRINTLN ("Base.getnumber int" +base);
return base;
}
public void Draw () {
System.out.println ("Base.draw");
}
}
public class Initializeorder extends base{
public int Age=getnumber (1001);
private int _radius=getnumber (10);
static int Sage=getnumber (250);
static{
System.out.println ("Subclass static Block");
}
{
System.out.println ("Subclass nonstatic block");
}
Initializeorder (int radius) {
_radius=radius;
System.out.println (age);
Draw ();//here is 1000.
System.out.println ("Initializeorder initialized");
}
public void Draw () {
System.out.println ("Initializeorder.draw" +_radius);
}
public static void Main (string[] args) {//TODO auto-generated method stub
New Initializeorder (1000);
}
}
The output is:
Base static block
Base.getnumber Int50
Base.getnumber int250
Subclass Static Block
Base.getnumber int100
Base nonstatic block
100
Base Start
Initializeorder.draw 0
Base End
Base.getnumber int1001
Base.getnumber int10
Subclass Nonstatic Block
1001
Initializeorder.draw 1000
Initializeorder initialized
Some of the execution order in Java, code block, static, construct, member .... (Of the Turn)