Code execution sequence in java

Keywords: Java Attribute

Previously, the execution sequence of static code blocks, construction code blocks, common code blocks, and class initialization has not been particularly clear. Here, collate, facilitate review!

Execution order:

  • Parent class - > subclass
  • Static Attribute - Static Code Block - > Common Attribute - Constructing Code Block - > Constructing Method - > Common Code Block (where method is called before execution)

Reason analysis:

Static code block:

  1. The code block {} declared with the static keyword in java.
    static {
        System.out.println("Static code block");
    }
  2. Code level Membership level
  3. == Execute at class loading and only once==
    For those who are not very clear about class loading, you can read this blog first. Class loading)
    class Parent {
    static {
        System.out.println("A");
    }
    Parent(){
        System.out.println("sdfsaf");
    }
    static {
        System.out.println("B");
    }
    }
    public class ExplicitStatic  {
    public static void main(String[] args) {
        new Parent();//The first execution triggered class loading
        System.out.println("=========A gorgeous partition line.=========");
        new Parent();//Second instantiation, no class loading is triggered, no static code block is executed
    }
    }

    Operation results:

Construction code block:

  1. A block of code defined directly in a class without a static keyword is called a {} constructor block.
    public class Test01 {
    {
        System.out.println("I'm building blocks of code.");
    }
    public static void main(String[] args) {
        new Test01();
    }
    }
  2. Code level: member level
  3. Constructing blocks are called when creating objects, and each time an object is created, it is called.
  4. Constructing code blocks takes precedence over class constructors
class Test02{
    {
        System.out.println("I am the parent constructor block");
    }
}

public class Test01 extends Test02{

    {
        System.out.println("I am a subclass constructor block 1");
    }

    Test01(){
        System.out.println("I am a parametric construction method.");
    }
    Test01(int v){
        System.out.println("I have a parametric construction method.");
    }

    static {
        System.out.println("Static code block");
    }

    {
        System.out.println("I am a subclass constructor block 2");
    }
    public static void main(String[] args) {
        new Test01();
        System.out.println("===================A gorgeous partition line.===================");
        new Test01(6);//Constructor blocks are executed every time an object is created
    }
}

Operation results:

Ordinary code block:

  1. The {} appearing in a method or statement is called a common code block.
    public void method() {
        {
            System.out.println("Common code block 1");
        }
        ....
    }
  2. Code Level: Method Level
  3. Ordinary code blocks and general statement execution order are determined by the order in which they appear in the code - "first appear first execute"
class Test02{
    {
        System.out.println("I am the parent constructor block");
    }
}

public class Test01 extends Test02{

    {
        System.out.println("I am a subclass constructor block");
    }

    static {
        System.out.println("Static code block");
    }

    public void method(){
        {
            System.out.println("Common code block 1");
        }

        System.out.println("General method body");

        {
            System.out.println("Common code block 2");
        }
    }
    public static void main(String[] args) {
        new Test01();
        System.out.println("===================A gorgeous partition line.===================");
        new Test01().method();//The code block in the method will be executed only if it is called.
    }
}

Operation results:

Class instantiation:

  1. Class instantiation has three parts: attributes, building blocks of code, and constructing methods.
  2. Attribute loading and construction code block loading are of the same level, and the execution order is writing order.
  3. The construction method is executed after loading in the first two ways

Conclusion:

The order in which objects are instantiated:

1. Static member variables and static code block loading of parent class

2. Static member variables and static code block loading of subclasses

3. Loading of parent class member variables and constructor blocks

4. Construction method loading of parent class

5. Subclass member variables and construction code block loading

6. Subclass Construction Method Loading

The test source code is as follows:

class Parent2 {
    {
        System.out.println("Parent class constructor block 1");//5
    }

    Parent2() {
        System.out.println("Parametric-free Construction of Parent Classes");//7
    }

    static {
        System.out.println("Static code block 1 of parent class");//1
    }

    {
        System.out.println("Parent Construction Code Block 2");//6
    }

    static {
        System.out.println("Static code block 2 of parent class");//2
    }
}

public class ExplicitStatic extends Parent2 {

    static {
        System.out.println("Static code block 1 for subclasses");//3
    }

    ExplicitStatic() {//15
        super();//Omission
        System.out.println("Subclass parametric-free construction method");//16
    }

    ExplicitStatic(int v) {//14
        this();//Execution 15
        System.out.println("Subclass parametric construction method");//17
    }

    private int initA() {
        System.out.println("Attribute initialization of subclasses calls common methods A");//9
        return 0;
    }

    static {
        System.out.println("Static code block 2 for subclasses");//4
    }

    private int initB() {
        System.out.println("Attribute initialization of subclasses calls common methods B");//12
        return 0;
    }

    int a = initA();//8

    {
        System.out.println("Construction code block 1 of subclass");//10
    }

    int b = initB();//11

    {
        System.out.println("Construction code block 2 of subclass");//13
    }

    public void method() {
        {
            System.out.println("Common code block 1 of subclass");
        }
        System.out.println("General methods of subclasses method");
        {
            System.out.println("Common code block 2 for subclasses");
        }
    }

    public static void main(String[] args) {
        new ExplicitStatic(100);
        System.out.println("=================");
        new ExplicitStatic().method();

}

The result of operation is as follows:

Posted by mikeabe on Mon, 14 Oct 2019 06:34:58 -0700