Load order of Java classes

Java Constructor, Constructor Code Block, Difference and Call Time of Static Code Block

public class Constructor {

    public static void main(String[] args) {
        System.out.println("创建第一个对象:");
        Test test1 = new Test();
        System.out.println("");
        System.out.println("创建第二个对象:");
        Test test2 = new Test(5);
    }
}

class Test {
    // 静态代码块1
    static {
        System.out.println("我是静态代码块1");
    }
    // 构造代码块1:
    {
        System.out.println("我是构造代码块1");
    }

    // 构造函数1
    public Test() {
        System.out.println("我是无参构造函数");
    }

    // 构造函数2
    public Test(int t) {
        System.out.println("我是带参构造函数," + "参数是" + t);
    }

    // 静态代码块2
    static {
        System.out.println("我是静态代码块2");
    }

    // 构造代码块2:
    {
        System.out.println("我是构造代码块2");
    }
}
创建第一个对象:
我是静态代码块1
我是静态代码块2
我是构造代码块1
我是构造代码块2
我是无参构造函数

创建第二个对象:
我是构造代码块1
我是构造代码块2
我是带参构造函数,参数是5

Execution time: Static code block > Constructor code block > Constructor

The static code block will only be loaded once when the class is loaded into the memory. The construction code block and the constructor are executed when the object is created, and several objects will be executed several times, so the process of loading the configuration file is generally written in in static code block

Load order of non-inherited classes

public class Parameter {

    public Parameter(String str) {
        System.out.println(str);
    }
}
public class Test1 {

    public static Parameter parameter1 = new Parameter("静态成员变量");

    public Parameter parameter2 = new Parameter("非静态成员变量");

    public Test1() {
        System.out.println("构造函数");
    }

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

    {
        System.out.println("构造代码块a");
    }

    {
        System.out.println("构造代码块b");
    }

    public static void main(String[] args) {
        /*
          静态成员变量
          静态代码块
          非静态成员变量
          构造代码块a
          构造代码块b
          构造函数
        */
        new Test1();
        System.out.println();
        /*
          非静态成员变量 
          构造代码块a
          构造代码块b
          构造函数
        */
        new Test1();
    }

}

You can see that static member variables, like static code blocks, will only be loaded once when the class is loaded into memory

public class Test2 {

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

    public Test2() {
        System.out.println("构造函数");
    }

    {
        System.out.println("构造代码块b");
    }

    {
        System.out.println("构造代码块a");
    }

    public static Parameter parameter1 = new Parameter("静态成员变量");

    public Parameter parameter2 = new Parameter("非静态成员变量");

    public static void main(String[] args) {
        /*
          静态代码块
          静态成员变量
          构造代码块b
          构造代码块a
          非静态成员变量
          构造函数
        */
        new Test2();
    }

}

Execution order (static functions are loaded when called)

  1. Static code blocks and static member variables, the loading order is determined by the writing order
  2. Construct code blocks and non-static member variables, the loading order is determined by the writing order
  3. Constructor

Load order of inherited classes

public class Father {

    public static Parameter parameter1 = new Parameter("父类静态成员变量");

    public Parameter parameter2 = new Parameter("父类非静态成员变量");

    public Father() {
        System.out.println("父类的构造函数");
    }
}
public class Son extends Father{

    public static Parameter parameter1 = new Parameter("子类静态成员变量");

    public Parameter parameter2 = new Parameter("子类非静态成员变量");

    public Son() {
        System.out.println("子类的构造函数");
    }

    public static void main(String[] args) {
        /*
          父类静态成员变量
          子类静态成员变量
          父类非静态成员变量
          父类的构造函数
          子类非静态成员变量
          子类的构造函数
        */
        new Son();
    }
}

Execution order
1. The static of the parent class (static code blocks, static member variables), the static of the child class (static code blocks, static member variables)
2. The non-static of the parent class (construction code blocks, non-static member variables), the parent class Class constructor
3. Subclass non-static (construction code block, non-static member variable), subclass constructor

Reference blog

Constructor execution time
[4] https://www.cnblogs.com/zavierliu/p/7599208.html
[5] http://www.cnblogs.com/mjyung/p/6708230.html
[6] https:/ /www.cnblogs.com/xyczero/p/4002786.html
[7] https://www.cnblogs.com/SirSmith/p/5536288.html
The order of java class records
[1] https://www.cnblogs. com/SirSmith/p/5536288.html
[2] http://blog.csdn.net/mrzhoug/article/details/51581994

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325428110&siteId=291194637