Static code block, construction code block, construction method execution order

Static code block, construction code block, construction method execution order

2.1 A little test.
First verify by yourself, and then check the answer to see where you think it is different.

public class StaticTest {
    
    
    public static StaticTest st = new StaticTest();
    public static StaticTest st2 = new StaticTest();

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

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

    public StaticTest() {
    
    
        System.out.println("无参构造");
    }

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

The execution result is as follows. Did you get it right?

Construction code block
No-parameter construction
Construction code block
No-parameter construction
Static code block
Construction code block
No-parameter construction

Wipe away the sad tears, let's watch

2.2 Theoretical knowledge is very important

Construction method syntax:

public class Student {
    
    
    /**
     * 无参构造方法
     */
    public Student() {
    
    
    }
}

What is the construction method?

The construction method is a special method, which is different from other methods.
1. The method name of the constructor must be exactly the same as the class name, there is no return type, not even void.
2. If the object is not created, the construction method will not run. The role of the constructor is to initialize the object.
3. It cannot be modified by static, final, synchronized, abstract and native. The construction method cannot be inherited by subclasses.

Structure code block syntax:

public class Student {
    
    
    /**
     * 构造代码块
     */
    {
    
    
    }
}

What is a structured code block?
Defined directly in the class, without any keyword modification, the code fragment enclosed by "{}" is the construction code block

The difference between construction method and construction code block

Same point:

  • Both the construction code block and the constructor initialize the object

difference:

  • The construction code block runs as soon as the object is created, and takes precedence over the construction method execution
  • The construction code block is to initialize all objects uniformly, and the construction method is to initialize the corresponding object, because there can be multiple construction methods, which construction method will create what kind of object, but no matter which object is created, it will be first Perform the same construction code block. In other words, what is defined in the construction code block is the initialization content common to different objects.

Static code block syntax:

public class Student {
    
    
    /**
     * 静态代码块
     */
    static {
    
    
    }
}

What is a static code block?

1. It is executed as the class is loaded, only once, and takes precedence over the main function. Specifically, static code blocks are called by classes. When the class is called, the static code block is executed first, and then the main function is executed.

2. The static code block is actually to initialize the class, and the construction code block is to initialize the object.

3. The variables in the static code block are local variables, and the nature of the local variables in the ordinary function is no different.

4. There can be multiple static code blocks in a class

2.3 refresher
constructor complete understanding, construct a block of code, static code block look back problems to begin with.

public class StaticTest {
    
    
    public static StaticTest st = new StaticTest();//实例化静态对象
    public static StaticTest st2 = new StaticTest();//实例化静态对象

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

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

    public StaticTest() {
    
    
        System.out.println("无参构造");
    }

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

Analysis: The
first step: instantiating static objects appears first in the class than static code blocks (executed only once as the JVM loads the class), so the former is executed first.
The second step: instantiate the static object to construct the code block and execute the construction method first.
Step 3: Enter the main method and execute the instantiated object.

2.4 What makes perfect

public class StaticTest {
    
    
    private static int age = 20;//静态变量
    private String name = "西北";//成员变量

    public static StaticTest st = new StaticTest();

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

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

    public StaticTest() {
    
    
        System.out.println("name=" + name);
        System.out.println("无参构造");
    }

    public static void main(String[] args) {
    
    
        System.out.println("这是main方法");
        new StaticTest();
    }
}

Results of the:

Construction code block
name=Northwest
No-parameter construction
age=20
Static code block
This is the main method
Construction code block
name=Northwest
No-parameter construction

2.5 Knowledge expansion (inheritance relationship)

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

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

operation result:

Father’s static code block
Son’s static code block
Father’s construction code block
Father’s construction method
Son’s construction code block
Son’s construction method

When it comes to inheritance, execute as follows:

1. Execute the static code block of the parent class and initialize the static variables of the parent class.
2. Execute the static code block of the subclass and initialize the static 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 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.

Finally, I summarize the article with a picture.
Insert picture description here

The above is the execution sequence of the static code block, the construction method, and the construction code block. If it's useful to you, remember to Sanlian.

Guess you like

Origin blog.csdn.net/lirui1212/article/details/115181419