Detailed explanation of the execution order of static code blocks, construction methods, construction code blocks, variables, and static variables

Static code block

​ Use the static statement, executed when the jvm loads the class. Only execute once

​ [Notes]: Remember one sentence, static things have nothing to do with objects

Structure code block

​ Use {} directly in the class. Execute every time an object is created.

Order of execution priority: static code block> construction code block> construction method

If in a class, there are member variables, construct code blocks, and construct methods.

Execution process: member variable | construction code block> construction method

[Notes]: 1. When the
java compiler compiles a source file, it will raise the declaration statement of the member variable to the top.
2. The initialization of member variables and the code of the code block will be moved to the construction method for execution.
3. The order of member variables and construction code blocks is mostly executed in the order of code. The construction method will be executed last.

Example

//比较构造代码块和构造方法的优先级
public class Demo1 {
    
    
    //成员变量
    public int num = 1000;
    //构造方法
    public Demo1() {
    
    
        this.num = 3000;
    }
    //构造代码块
    {
    
    
        this.num = 2000;
    }

    public static void main(String[] args) {
    
    
        Demo1 demo1 = new Demo1();
        System.out.println(demo1.num);
    }
}

operation result:

3000

Construction method

		public Hello() {
			}

[Notes]:
1. Once the object is created, the corresponding construction method will be called. In other words, the object is not created, and the construction method is not executed.
2. Initialize the object.
3. When an object is created, the construction method is run only once, and then an object is created and run again.

Structure code block

{ Code block; } [Notes]: 1. Constructing a code block is to initialize the object. 2. The construction code block is run as soon as the object is established, and it takes precedence over the construction method. Emphasize! ! ! : Only an object can execute the structured code block. 3. The class cannot call the construction code block.





Static code block

static { code block; } [Notes] 1. It is executed with the loading of the class, and only executed once, and takes precedence over the main function. Specifically, static code blocks are called by classes. 2. The static code block initializes the class, and the construction code block initializes the object. 3. The variable in the static code is a local variable, which is no different from a normal local variable. 4. There can be multiple static code blocks in a class.






Example

public class Demo2 {
    
    

    //成员变量
    static int cut = 6;

    //静态代码块
    static  {
    
    
        cut += 9;
    }

    public static void main(String[] args) {
    
    
        System.out.println(cut);
    }

    //静态代码块
    static {
    
    
        cut /= 3;
    }
}

Results of the:

5

Initial order of java classes

For a class, the static code block is executed as long as the class is loaded

Example 1

public class HelloA {
    
    

    public HelloA() {
    
    
        System.out.println("构造方法");
    }

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

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

    public static void main(String[] args) {
    
    
    
    }
}

operation result:

静态代码块

Example 2

public class HelloA {
    
    

    public HelloA() {
    
    
        System.out.println("构造方法");
    }

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

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

    public static void main(String[] args) {
    
    
        HelloA helloA = new HelloA();
    }
}

operation result:

静态代码块
构造代码块
构造方法

Example 3

public class HelloA {
    
    

    public HelloA() {
    
    
        System.out.println("构造方法");
    }

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

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

    public static void main(String[] args) {
    
    
        HelloA helloA = new HelloA();
        HelloA helloA1 = new HelloA();
    }
}

operation result:

静态代码块
构造代码块
构造方法
构造代码块
构造方法

Variables and static variables

1. First go to execute the static code block

2. Execute the construction code block

3. Execute the construction method

For static variables, static code blocks, variables, code blocks, construction methods. The order of their initialization: (static variable, static code block)>(variable, code block)>construction method

Example

public class InitTest {
    
    
    //静态变量
    public static String staticField = "静态变量";
    //变量
    public String field = "变量";
    //静态代码块
    static {
    
    
        System.out.println(staticField);
        System.out.println("静态代码块");
    }
    //代码块
    {
    
    
        System.out.println(field);
        System.out.println("代码块");
    }

    public InitTest() {
    
    
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
    
    
        new InitTest();
    }
}

operation result:

静态变量
静态代码块
变量
代码块
构造方法

Inheritance class

Example

class HelloB {
    
    

    public HelloB() {
    
    
        System.out.println("B的构造方法");
    }

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

    static {
    
    
        System.out.println("B的静态代码块");
    }
}
class HelloC extends HelloB {
    
    
    public HelloC() {
    
    
        System.out.println("C的构造方法");
    }

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

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

    public static void main(String[] args) {
    
    
        HelloC helloC = new HelloC();
        System.out.println("**********");
        HelloC helloC1 = new HelloC();
    }
}

operation result:

B的静态代码块
C的静态代码块
B的构造代码块
B的构造方法
C的构造代码块
C的构造方法
**********
B的构造代码块
B的构造方法
C的构造代码块
C的构造方法

When it comes to inheritance, the sequence is as follows:
1. Execute the static code block of the parent class. And initialize the static member variables of the parent class
2. Execute the static code block of the subclass. And initialize the static member variables of the
subclass 3. Execute the construction code block of the parent class, execute the construction method of the parent class, and initialize the ordinary member variables of the parent class
4 execute the construction code block of the subclass, execute the construction method of the subclass, And initialize the member variables of the subclass

Guess you like

Origin blog.csdn.net/xue_yun_xiang/article/details/114590592