Say first summarized

Superclass static Fields – > superclass static code blocks – > subclass static fields – > subclass static code blocks – > Superclass member variables and non-static blocks (loaded sequentially) – > Superclass constructor – > Subclass member variables and non-static blocks (loaded sequentially) – > subclass constructor

  • Static code block: Declared with STAITC, executed only once when the JVM loads the class.
  • Construct code block: Class directly defined with {}, executed each time an object is created.
  • Execution order priority: static block,main(), constructor block, constructor method.

The constructor

 public HelloA(a){// constructor
 }
Copy the code

There are a few things to note about constructors:

  1. As soon as the object is created, the corresponding constructor is called. That is, the constructor will not run until the object is created.
  2. The constructor is used to initialize an object.
  3. When an object is created, the constructor is run only once, whereas ordinary methods can be called multiple times by the object.

Construct code block

 {// Construct the code block
 }
Copy the code

Here are a few things to note about constructing code blocks:

  1. The constructor block is used to initialize the object.
  2. The constructor block is run as soon as the object is created and takes precedence over the constructor. It is important to note that constructor blocks are run only when an object is created, and cannot be called by a class. Constructor blocks and constructors are executed before each other.
  3. : is the difference between tectonic blocks of code and the constructor structure block of code is for all objects unity initialization, and the constructor is to give the corresponding object initialization, because the constructor can be multiple, which the constructor will run to establish what kind of object, but no matter which object, will perform the same tectonic blocks of code first. That is, the initialization content that is common to different objects is defined in the construction code block.

Static code block

 static {// Static code block
 }
Copy the code

Something to note about static code blocks:

  1. It is executed once as the class is loaded and takes precedence over the main function. In particular, static code blocks are invoked by classes. Class call, the static block of code is executed before the main function’s.
  2. A static code block initializes a class, whereas a construction code block initializes an object.
  3. Variables in static code blocks are local variables, no different in nature from local variables in ordinary functions.
  4. You can have more than one static code block in a class.
 public class Test {
     staitc int cnt=6;
  
     static {
         cnt += 9;
     }
  
     public static void main(String[] args) {
         System.out.println(cnt);
     }
  
     static {
         cnt /= 3; }}/ / 5
Copy the code

Java class initialization order

For a class

 public class HelloA {
     public HelloA(a){// constructor
         System.out.println("Constructor of A");    
     }
     
     {// Construct the code block
         System.out.println("Construction code block of A");    
     }
     
     static {// Static code block
         System.out.println("Static code block of A");        
     }
     
     public static void main(String[] args) {}}// A static code block
Copy the code
 public class HelloA {
     public HelloA(a){// constructor
         System.out.println("Constructor of A");    
     }
     
     {// Construct the code block
         System.out.println("Construction code block of A");    
     }
     
     static {// Static code block
         System.out.println("Static code block of A");        
     }
     
     public static void main(String[] args) {
         HelloA a=newHelloA(); }}// A static code block
 // construct A block of code
 // the constructor of A
Copy the code
 public class HelloA {
     public HelloA(a){// constructor
         System.out.println("Constructor of A");    
     }
     
     {// Construct the code block
         System.out.println("Construction code block of A");    
     }
     
     static {// Static code block
         System.out.println("Static code block of A");        
     }
     
     public static void main(String[] args) {
         HelloA a=new HelloA();
         HelloA b=newHelloA(); }}// A static code block
 // construct A block of code
 // the constructor of A
 // construct A block of code
 // the constructor of A
Copy the code

For a class, execute in the following order:

  1. Execute a static code block
  2. Execute the construct code block
  3. Execute constructor

For static variables, statically initializing blocks, variables, initializing blocks, constructors, the initialization order is static variables, statically initializing blocks) > (variables, initializing blocks) > constructors.

 public class InitialOrderTest {
         /* Static variable */
     public static String staticField = "Static variable";
         / * * / variable
     public String field = "Variable";
         /* Static initializer 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(a){
         System.out.println( "Constructor" );
     }
  
     public static void main( String[] args ){
         newInitialOrderTest(); }}// Static variables
 // Statically initialize the block
 / / variable
 // Initialize the block
 / / the constructor
Copy the code

For inheritance

public class HelloA {
    public HelloA(a){// constructor
        System.out.println("Constructor of A");    
    }
    {// Construct the code block
        System.out.println("Construction code block of A");    
    }
    static {// Static code block
        System.out.println("Static code block of A"); }}public class HelloB extends HelloA{
    public HelloB(a){// constructor
        System.out.println("Constructor of B");    
    }
    {// Construct the code block
        System.out.println("Construction code block of B");    
    }
    static {// Static code block
        System.out.println("Static code block for B");        
    }
    public static void main(String[] args) {
        HelloB b=newHelloB(); }}// A static code block
// static code block for B
// construct A block of code
// the constructor of A
// build code block for B
// the constructor of B
Copy the code

When it comes to inheritance, execute in the following order:

  1. Executes the static code block of the parent class and initializes the static member variable of the parent class
  2. Executes the subclass’s static code block and initializes the subclass’s static member variables
  3. Executes the constructor block of the parent class, executes the constructor of the parent class, and initializes the normal member variable of the parent class
  4. Executes the subclass’s construction code block, executes the subclass’s constructor, and initializes the subclass’s ordinary member variables
class Parent {
        /* Static variable */
    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 initializer block */
    static {
        System.out.println( p_StaticField );
        System.out.println( "Parent class -- Static Initializer block" );
    }
        /* Initialize the block */
    {
        System.out.println( p_Field );
        System.out.println( "Parent class -- Initializer block" );
    }
        /* Constructor */
    public Parent(a){
        System.out.println( "Parent class -- Constructor" );
        System.out.println( "i=" + i + ", j=" + j );
        j = 20; }}public class SubClass extends Parent {
         /* Static variable */
    public static String s_StaticField = "Subclass -- Static variable";
         / * * / variable
    public String s_Field = "Subclass -- Variable";
        /* Static initializer block */
    static {
        System.out.println( s_StaticField );
        System.out.println( "Subclass - Static Initializer block" );
    }
       /* Initialize the block */
    {
        System.out.println( s_Field );
        System.out.println( "Subclass -- Initializer block" );
    }
       /* Constructor */
    public SubClass(a){
        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" );
        newSubClass(); }}// Parent class -- static variable
// Parent class -- static initializer block
// Subclass -- static variable
// Subclass - static initializer block
// Subclass main
// Parent class -- variable
// Parent class -- initializer block
// Parent class -- constructor
// i=9, j=0
// Subclass -- variable
// Subclass -- initialize block
// Subclass -- constructor
// i=9,j=20
Copy the code

The initialization of static variables and static initializer blocks of subclasses is done before the initialization of variables, initializer blocks, and constructors of the parent class. Static variables, static initializer blocks. The order in which variables and initializer blocks are initialized depends on the order in which they appear in the class.