Home  >  Article  >  Java  >  The order in which java objects are initialized

The order in which java objects are initialized

巴扎黑
巴扎黑Original
2017-07-23 13:46:401617browse

1. Several parts involved in initialization

The Java object not only calls its own constructor during initialization, but also involves static statement blocks , instance statement block, static variable initialization, member variable initialization and other execution issues. The initialization sequence refers to the execution sequence of these five parts.

2.Demo

package com.javase.classtest;public class InitializationSequence {static {
        System.out.println("静态语句块");
    }static MyInnerClass in = new MyInnerClass("静态变量s");

    MyInnerClass in01 = new MyInnerClass("a");

    {
        System.out.println("实例语句块");
    }

    MyInnerClass in02 = new MyInnerClass("b");public InitializationSequence() {
        System.out.println("构造函数");
    }

    @SuppressWarnings("unused")public static void main(String[] args) {
        InitializationSequence obj01 = new InitializationSequence();
        System.out.println("-----------------------------------");
        InitializationSequence obj02 = new InitializationSequence();
    }

}class MyInnerClass {public MyInnerClass(String description) {
        System.out.println("成员变量初始化::" + description);
    }
}

Execution result:

Analysis:

  1. Static statement block: executed when the class is loaded into the JVM, because the prerequisite for the JVM to use the class is that the class is loaded into JVM virtual machine, so the static statement block is executed first, the class is only loaded once, and the static statement block is only executed once.

  2. Static variable initialization: Since static variables are shared by all instances of the class and do not depend on specific objects, they are not initialized when the object is created, but Initialized when the class is loaded, the initialization sequence is after the static statement block and is only executed once.

  3. Instance statement block: executed before the constructor method, executed once each time the constructor method is called. The execution order is after the initialization of static variables, with reference to the initialization of member variables. There is no absolute order. The actual order depends on the order of definition. If it is defined first, it will be executed before the member variable is initialized; if it is defined later, it will be executed after the member variable is initialized. executed later.

  4. Member variable initialization: executed before the constructor, and executed once each time the constructor is called. Execution order After initialization of static variables, the execution order relative to instance statement blocks depends on the definition order.

  5. Construction method: executed last.

It can be seen from the above that, the execution order of Java object initialization is: static statement block>static variable initialization>instance statement block/member variable initialization >The relative execution order of constructors, instance statement blocks and member variable initialization depends on the order of definition.

The above is the detailed content of The order in which java objects are initialized. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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