Execution order of static code blocks, instance code blocks, and constructor methods in the class

    When the class is loaded, the statements in the static code will be executed, only once during the entire class loading process, and it has nothing to do with how many objects are created.

    The statements in the instance code block will be added before the first statement of the constructor method after compilation.

    Construction method: executed every time an object is created

Give a simple example

public class Person {
    public String name;
    public String gender;
    public int age;
    public Person(String name,String gender,int age){
        this.name =name;
        this.gender = gender;
        this.age = age;
        System.out.println("基类的构造方法");
    }

    {
        System.out.println("基类的实例代码块");
    }

    static {
        System.out.println("基类中的静态代码块");
    }
}
public class Student extends Person{
    public int id;

    public Student(String name,String gender,int age,int id){
        super(name,gender,age);
        this.id = id;
        System.out.println("子类的构造方法");
    }

    {
        System.out.println("子类的实例代码块");
    }

    static {
        System.out.println("子类的静态代码块");
    }

    public static void main(String[] args) {
        Student s1 = new Student("张三","男",24,11111);
        System.out.println("=================================");
        Student s2 = new Student("李四","男",35,11668);
    }
}

The execution result is:

By analyzing the execution results, the following conclusions are drawn:

1. The static code block of the parent class takes precedence over the static code block of the subclass and is executed earliest.

2. The parent class instance method and the parent class constructor method are executed immediately.

3. The instance code block of the subclass and the construction method of the subclass are executed immediately.

4. When the object is instantiated for the second time, the static code blocks of the parent class and subclass will no longer be executed.

5. The static code block is executed first and only once, during the class loading phase.

6. If the class is not loaded, load the class first: load the base class first, then load the subclass . The static code block of the base class is executed first, and the code block of the subclass is executed later, and they are only executed once.

7. When an object is created, the instance code block will be executed. After the instance code block is executed, the final constructor method will be executed.

  Notice:

   1. New object of which class, the compiler will call the constructor of that class

   2. new subclass object: the compiler will call the constructor of the subclass

        a. Member variables inherited from the base class call the base class constructor through super()

        b. Newly added members of subclasses

   3. The content in the base class construction method is printed first, and then the content in the subclass construction method is printed last.

   However: You cannot print the result to indicate that the constructor of the base class is called first, and then the constructor of the subclass is called (wrong statement )

Guess you like

Origin blog.csdn.net/m0_53677355/article/details/126598933