JAVA virtual machine: object creation process

Briefly, the Java object creation process is divided into the following steps:

1. Perform relevant inspections;

2. Allocate memory for the object, and initialize the allocated memory space to zero value;

3. Initialize the construction code block and the constructor

 These steps are detailed below:

1. Perform relevant inspections

When the virtual machine encounters a new instruction, it will check whether the parameters of this instruction can locate a symbolic reference of a class in the constant pool, and check whether the class represented by the symbolic reference has been loaded, resolved and initialized. No, then the corresponding class loading process must be executed first;

例:Person p = new Person()

When there is such a new instruction, we first check whether the symbolic reference p can be located in the constant pool, and then check whether the Person class has been loaded, parsed and initialized, and if not, perform the relevant operations; the static code block is is executed during this loading process;

2. Allocate memory for the object

After the class loading check is completed, memory will be allocated for the new object;

Initialize the allocated memory space to zero value;

If the Person class has an inherited parent class, allocating memory in this step will allocate the memory of the Person class and its parent class, and first complete the creation of the Person parent class object

3. Initialize the construction code block and the constructor

After completing the initialization of the memory space in the previous step, proceed to the initialization of the construction code block and the constructor

Look at the following example to understand the process of object creation

public class test extends Person{
    static {
        System.out.println( "Subclass static code block" );
    }
    {
        System.out.println( "Subclass constructs code block" );
    }
    public test() {
        System.out.println( "Subclass constructor" );
    }
    public static void main(String[] args) {
        test t = new test();
    }
}

class Person{
    static{
        System.out.println( "Parent class static code block" );
    }
    {
        System.out.println( "Parent class construction code block" );
    }
    Person(){
        System.out.println( "Parent class constructor" );
    }
}
Parent class static code block
Subclass static code block
Parent class construction code block
Parent class constructor
Subclass construction code block
Subclass constructor

From the results it can be seen that:

1. The static code block is executed first, and the static code blocks of the subclass and parent class are executed during the class loading process;

2. The creation of the parent class object is completed before the subclass object, so the construction code block and construction method of the parent class are executed first, and then the subclass is executed after execution;

3. The construction code block is executed before the constructor. In fact, the loading order of the entire object is: the static code block of the parent class and the subclass, the variables in the parent class, the code block, the construction method, and then initialize the subclass variables, Code blocks, constructors. Among them, the variables are initialized and created first, followed by the code blocks, and the constructors are created last;

 

Guess you like

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