Home > Java > JavaBase > What is the execution order of static code blocks, construction code blocks, and construction methods?

What is the execution order of static code blocks, construction code blocks, and construction methods?

青灯夜游
Release: 2022-01-25 11:41:17
Original
15088 people have browsed it

The execution order is: first execute the "static code block", then execute the "constructed code block", and finally execute the "constructed code block". Static code blocks are at the class level, while structural code blocks and construction methods are at the instance level, so the static code blocks are executed first; and because the structural code blocks are independent and must rely on a carrier to run, the structural code blocks need to be placed in the structure before method.

What is the execution order of static code blocks, construction code blocks, and construction methods?

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

Construction code block, static code block and construction method are three different code blocks in the class, so what are the differences between them?

1. Introduction to three types of code blocks

Static code block: declared with staitc, executed when jvm loads the class, only executed once

Constructed code block: directly used in the class {} definition, executed every time an object is created.

Execution order priority: static block, main(), construction block, construction method.

class A
{
    //构造代码块
    {
        System.out.println("构造代码块A");
    }
    //静态代码块
    static
    {
        System.out.println("静态代码块A");
    }
    //构造方法
    public A()
    {
        System.out.println("构造方法A");
    }
}
Copy after login

The execution order of the second and third parties

In order to clarify the execution order of the three, we instantiate class A and test single instances and multiple instances respectively.

2.1 Single instance

class Demo
{
    public static void main(String[] args)
    {
        new A();
    }
}
Copy after login

2.2 Multiple instances

class Demo
{
    public static void main(String[] args)
    {
        new A();
        new A();
        new A();
    }
}
Copy after login

3. The order in the inheritance system

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

class Demo
{
    public static void main(String[] args)
    {
        new B();
    }
}
Copy after login

4. Summary

1. In the process of creating an object, the execution order of the three is: Static code block --> Construction code block --> Construction method;

1. Static code block: It is performed during the third step of initialization of the class loading process. Its main purpose is Is to assign an initial value to the class variable.
2. Construction code block: It is independent and must be attached to a carrier to run. Java will place the construction code block in front of each construction method to instantiate some common instance variables and reduce the amount of code.
3.Construction method: used to instantiate variables.

1 is at the class level, 2 and 3 are at the instance level, so naturally 1 takes precedence over 23.

Let’s understand one thing: the active use of a subclass will lead to the active use of its parent class, so although the subclass is instantiated, it will also lead to the initialization and instantiation of the parent class, and it is optimal Executed in subclasses.

2. Every time an object is created, the construction code block and construction method will be executed once; no matter how many objects are created, the static code block will only be executed once when the first object is created;
3. When creating a subclass object, the static code block of the subclass is executed after the static code block of the parent class, but takes precedence over the construction code block and construction method of the parent class;
4. When creating a subclass object, The construction code block of the subclass is executed after the construction method of the parent class.

Recommended related video tutorials: Java video tutorial

The above is the detailed content of What is the execution order of static code blocks, construction code blocks, and construction methods?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template