Java code block execution order

Posted by charmedp3 on Sat, 18 Dec 2021 05:29:06 +0100

Let's start with the summary

Parent static field - > parent static code block - > child static field - > child static code block - > parent member variable and non static block (sequential loading) - > parent constructor - > child member variable and non static block (sequential loading) - > child constructor

  • Static code block: declared with staitc, executed only once when the jvm loads the class.
  • Construction code block: it is directly defined with {} in the class and executed every time an object is created.
  • Execution order priority: static block, main(), construction block, construction method.

Constructor

public HelloA(){//Constructor
}

For constructors, the following points should be noted:

  1. As soon as the object is established, the corresponding constructor will be called, that is, the constructor will not run if the object is not established.
  2. The constructor is used to initialize an object.
  3. When an object is established, the constructor runs only once, while a general method can be called multiple times by the object.

Construct code block

{//Construct code block    
}

The following points should be noted about constructing code blocks:

  1. The purpose of constructing code blocks is to initialize objects.
  2. The construction code block runs as soon as the object is established, and takes precedence over the constructor execution. It should be emphasized here that the construction code block will be run only after the object is established. The class cannot call the construction code block, and the execution order of the construction code block and the constructor is that the former is executed before the latter.
  3. The difference between construction code block and constructor is that construction code block initializes all objects uniformly, while constructor initializes corresponding objects, because there can be multiple constructors. Which constructor will create what kind of object, but no matter which object is created, the same construction code block will be executed first. In other words, the initialization contents common to different objects are defined in the construction code block.

Static code block

static {//Static code block    
}

For static code blocks, note that:

  1. It is executed as the class is loaded, only once, and takes precedence over the main function. Specifically, static code blocks are called by classes. Class call, execute the static code block first, and then execute the main function.
  2. Static code blocks are actually initialized for classes, while constructed code blocks are initialized for objects.
  3. Variables in static code blocks are local variables, which are no different from local variables in ordinary functions.
  4. There can be multiple static code blocks 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

Java class initialization order

For a class

public class HelloA {
    public HelloA(){//Constructor
        System.out.println("A Constructor for");    
    }
    
    {//Construct code block
        System.out.println("A Construction code block for");    
    }
    
    static {//Static code block
        System.out.println("A Static code block for");        
    }
    
    public static void main(String[] args) {
    }
}

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

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

For a class, execute in the following order:

  1. Execute static code block
  2. Execute construction code block
  3. Execute constructor

For static variable, static initialization block, variable, initialization block and constructor, their initialization order is static variable, static initialization block) > (variable, initialization block) > constructor.

public class InitialOrderTest {
        /* Static variable */
    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" );
    }
        /* Initialization 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();
    }
}

// Static variable
// Static initialization block
// variable
// Initialization block
// constructor 

For inheritance

public class HelloA {
    public HelloA(){//Constructor
        System.out.println("A Constructor for");    
    }
    {//Construct code block
        System.out.println("A Construction code block for");    
    }
    static {//Static code block
        System.out.println("A Static code block for");        
    }
}

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

When inheritance is involved, execute in the following order:

  1. Execute the static code block of the parent class and initialize the static member variable of the parent class
  2. Execute the static code block of the subclass and initialize the static member variable of the subclass
  3. Execute the construction code block of the parent class, execute the constructor of the parent class, and initialize the common member variables of the parent class
  4. Execute the construction code block of the subclass, execute the constructor of the subclass, and initialize the common member variables of the subclass
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 initialization block */
    static {
        System.out.println( p_StaticField );
        System.out.println( "Parent class--Static initialization block" );
    }
        /* Initialization block */
    {
        System.out.println( p_Field );
        System.out.println( "Parent class--Initialization block" );
    }
        /* constructor  */
    public Parent(){
        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 initialization block */
    static {
        System.out.println( s_StaticField );
        System.out.println( "Subclass--Static initialization block" );
    }
       /* Initialization block */
    {
        System.out.println( s_Field );
        System.out.println( "Subclass--Initialization block" );
    }
       /* constructor  */
    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();
    }
}
// Parent class -- static variable
// Parent class -- static initialization block
// Subclass -- static variable
// Subclass -- static initialization block
// Subclass main method
// Parent -- variable
// Parent class -- initialization block
// Parent class -- constructor
// i=9, j=0
// Subclass -- variable
// Subclass -- initialization block
// Subclass -- constructor
// i=9,j=20

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

Topics: Java Interview