Loading order when java object is created (static method, member method, construction method, etc.)

The loading sequence of the code when the object is created is : static code -> non-static code -> construction method.

If the parent class is inherited, the loading sequence is : static code of the parent class -> static code of the child class -> non-static code inside the parent class -> construction method of the parent class -> non-static code of the child class -> The construction method of the subclass.

Among them, static code includes (static method, static variable, static code block, etc.), non-static code (member method, member variable, member code block, etc.) , the same kind of code, written on it, load first.

The loading sequence of the code when the object is created: as follows

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Parentclass Parentclass = new Parentclass();
    }

}
class Parentclass{
    
    
    static{
    
    
        System.out.println("父类静态代码块1");
    }
    static String tem=aa();

    static String aa(){
    
    
        System.out.println("父类的静态方法");
        return "";
    }
    public Parentclass(){
    
    
        System.out.println("父类的构造方法");
    }
    static{
    
    
        System.out.println("父类静态代码块2");
    }

    {
    
    
        System.out.println("父类非静态代码块1");
    }

    String temp = tt();
    String tt(){
    
    
        System.out.println("父类的成员方法");
        return "成员方法";
    }
    {
    
    
        System.out.println("父类非静态代码块2");
    }
}

The result of the execution is:

父类静态代码块1
父类的静态方法
父类静态代码块2
父类非静态代码块1
父类的成员方法
父类非静态代码块2
父类的构造方法

If the parent class is inherited, the loading sequence is:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Childernclass childernclass = new Childernclass();
    }

}
class Parentclass{
    
    
    static{
    
    
        System.out.println("父类静态代码块1");
    }
    static String tem=aa();

    static String aa(){
    
    
        System.out.println("父类的静态方法");
        return "";
    }
    public Parentclass(){
    
    
        System.out.println("父类的构造方法");
    }
    static{
    
    
        System.out.println("父类静态代码块2");
    }

    {
    
    
        System.out.println("父类非静态代码块1");
    }

    String temp = tt();
    String tt(){
    
    
        System.out.println("父类的成员方法");
        return "成员方法";
    }
    {
    
    
        System.out.println("父类非静态代码块2");
    }
}


class Childernclass extends Parentclass{
    
    
    static{
    
    
        System.out.println("子类静态代码块1");
    }
    static String tem=aa();

    static String aa(){
    
    
        System.out.println("子类的静态方法");
        return "";
    }
    public Childernclass(){
    
    
        System.out.println("子类的构造方法");
    }
    static{
    
    
        System.out.println("子类静态代码块2");
    }

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

    String temp = TT();
    String TT(){
    
    
        System.out.println("子类的成员方法");
        return "成员方法";
    }
    {
    
    
        System.out.println("子类非静态代码块2");
    }
}

父类静态代码块1
父类的静态方法
父类静态代码块2
子类静态代码块1
子类的静态方法
子类静态代码块2
父类非静态代码块1
父类的成员方法
父类非静态代码块2
父类的构造方法
子类非静态代码块1
子类的成员方法
子类非静态代码块2
子类的构造方法

Summary:
If the class has not been loaded:
1. Execute the static code block and static variable initialization of the parent class first, and the execution order of the static code block and static variable is only related to the order of appearance in the code.
2. Execute the static code block and static variable initialization of the subclass.
3. Execute the instance variable initialization of the parent class.
4. Execute the constructor of the parent class.
5. Execute the instance variable initialization of the
child class. 6. Execute the constructor of the child class.

If the class has been loaded:
the static code block and static variables do not need to be executed repeatedly. When the class object is created, only the variable initialization and construction methods related to the instance are executed.

Guess you like

Origin blog.csdn.net/qq_2662385590/article/details/110519381