Java Object initialization sequence USES detail

  • 2020-06-23 00:21:42
  • OfStack

1. Code block concept

Before exploring the object initialization sequence, let's look at the concept of a block of code.


class Test{
  public static String str1; // Static field 

  public String str2;     // Common field 

  static{           
    // Static code block 
  }

  {
    // Construction code block 
  }

  public Test() { 
    // The constructor 
  }
}

2. The order in which objects are initialized when subclasses are created

1. Field initialization, code block and constructor execution order

Let's look at the code and the result first


public class CodeBlockTest {
  public static void main(String[] args) {
    Child child = new Child();
  }
}
class Father {
  public static String fatherStr1 = "fatherStr1 (Static field initialization values) ";

  public String fatherStr2 = "fatherStr2 (Field initialization values) ";
  static {
    System.out.println(" Parent static code block: " + fatherStr1);
    fatherStr1 = "fatherStr1 (Static code block assignment) ";
  }
  {
    System.out.println(" Superclass construction code block: " + fatherStr2);
    fatherStr2 = "fatherStr2 (Construct code block assignment) ";
  }
  public Father() {
    System.out.println(" Superclass constructor block: " + fatherStr2);
    fatherStr2 = "fatherStr2 (Constructor assignment) ";
  }
}
class Child extends Father {
  public static String childStr1 = "childStr1 (Static field initialization values) ";
  public String childStr2 = "childStr2 (Field initialization values) ";
  static {
    System.out.println(" Subclass static code block: " + childStr1);
    childStr1 = "childStr1 (Static code block assignment) ";
  }
  {
    System.out.println(" Subclasses construct code blocks: " + childStr2);
    childStr2 = "childStr2 (Construct code block assignment) ";
  }
  public Child() {
    System.out.println(" Subclass constructor: " + childStr2);
    childStr2 = "childStr2 (Constructor assignment) ";
  }
}
//   Output results: 
//   Parent static code block: fatherStr1 (Static field initialization values) 
//   Subclass static code block: childStr1 (Static field initialization values) 
//   Superclass construction code block: fatherStr2 (Field initialization values) 
//   Superclass constructor block: fatherStr2 (Construct code block assignment) 
//   Subclasses construct code blocks: childStr2 (Field initialization values) 
//   Subclass constructor: childStr2 (Construct code block assignment) 

Explore the initialization order of objects by printing the value of the field after the execution of the previous 1 block of code, for every 1 block or constructor executed.

According to the current output, we can draw the following conclusions about the initialization order of objects:

1. Parent static field initialization

2. Superclass static block, subclass static field initialization (explore the order of the two next)

3. Subclass static code block

4. Generic field initialization of parent class

5. The parent class constructs the code block

6. Superclass constructor

7. Subclass plain field initialization

8. Subclasses construct code blocks

9. Subclass constructor

2. Execution order of parent static code block and child static field initialization

Again, let's explore the execution order between the two by looking at the execution results of the code.


public class CodeBloacTest2 {
  public static void main(String[] args) {
    Child child = new Child();
  }
}
class Father {
  public static String fatherStr = " (Static field initialization values) ";
  static {
    System.out.println(" Parent static code block: fatherStr" + fatherStr);
    fatherStr = " (Static code block assignment) ";
  }
}
class Child extends Father {
  public static String childStr = fatherStr;
  static {
    System.out.println(" Subclass static code block: childStr = fatherStr" + childStr);
    childStr = " (Static code block assignment) ";
  }
}
//   Output results: 
//   Parent static code block: fatherStr (Static field initialization values) 
//   Subclass static code block: childStr = fatherStr (Static code block assignment) 

When we initialize the subclass static field childStr, we assign the value of the parent static field fatherStr. According to the output, the value of childStr initialized is the value assigned to fatherStr after the static code block of the parent class executes. It can be seen that the execution order of the two is: superclass static code block == > Subclass static field initialization

Conclusion 3.

Parent class static field initialization

Parent static code block

Subclass static field initialization

Subclass static code block

Parent class ordinary field initialization

The parent class constructs the code block

Superclass constructors

Subclass ordinary field initialization

Subclasses construct blocks of code

Subclass constructor

It is obvious from the conclusion that static fields, blocks of code are executed in order over non-static fields, blocks of code. This is because in the static field belongs to the class, after the class load is 1 straight existence; A normal domain needs to create an object to access it. When an object is created, the parent class is loaded before the child class, so the static field initialization and static block execution of the parent class precedes the child class.

Above content hopes to be helpful to each friend


Related articles: