Class initialization process and object creation process

If you are interested in learning more about it, please visit my personal website: Yetong Space

Interviews are often encountered, so record them.

class initialization process

The initialization process of a Java class can be divided into the following steps:

  • Loading: When a Java program wants to use a class, the system will first check whether the class has been loaded into memory. If not, the system will load the .class file of the class into the memory through ClassLoader, and create a Class object in the heap area to describe the class.
  • Link
    • Verification: After the loading is complete, the system will verify the bytecode of the class to ensure that it complies with the Java Virtual Machine Specification and security requirements. It mainly includes the following contents: syntax checking, semantic checking, bytecode verification, symbol reference verification, etc.
    • Preparation: After the verification is passed, the system will allocate memory space for the static variables of this class and set default values. This process is carried out in the heap area, not in the stack area.
    • Resolution: After the preparation phase is completed, the system will perform symbolic reference resolution on the class, converting all symbolic references into direct references. This process includes constant pool analysis, class or interface analysis, field analysis and method analysis.
  • Initialization: After preparation and parsing are complete, the system begins to perform class initialization. This process is executed sequentially in the order in which it appears in the code, including operations such as assignment of static member variables and execution of static code blocks. It should be noted that class initialization will only be performed once.

The above is the initialization process of the Java class. It should be noted that the initialization method of a class cannot be called directly at any time, because the initialization method will only be executed when the class is loaded into memory and needs to be used.

object creation process

The process of creating a Java object can be divided into the following steps:

  • Class loading: In a Java program, when we need to use a certain class, the system will load the .class file of the class into memory through ClassLoader, and create a Class object to describe the class. During this process, the class is validated, parsed, and initialized. This step is not performed if the class has already been loaded.
  • Allocating memory: When the system creates an object through the new keyword or the reflection mechanism, the JVM will allocate a continuous memory space for the object in the heap area. It should be noted that since the heap area is shared memory, the process of allocating memory may be affected by factors such as the garbage collection mechanism.
  • Initialize zero value: After allocating memory, JVM will initialize all attribute variables (including instance variables and static variables) to default values, such as int type to 0, Object type to null, etc.
  • Set the object header: Each Java object has an object header, which is used to store the object's metadata information, such as the object's hash value, lock status, etc. When setting the object header, the JVM will choose different layout methods according to the type and size of the object.
  • Execute the construction method: After completing the above steps, the JVM will call the object's construction method to initialize the object and assign initial values ​​​​to the attribute variables. The execution order of the construction method is from parent class to subclass, from non-static member variables to static member variables.
  • Return object reference: Finally, the JVM returns the address of the object in memory to the program, and the program accesses and manipulates the object through this address.

It should be noted that the process of creating an object in Java is usually done automatically, we only need to use the new keyword or the reflection mechanism to trigger the creation of the object. At the same time, the destruction of Java objects is handled by the garbage collector, without the need to manually destroy objects.

Guess you like

Origin blog.csdn.net/tongkongyu/article/details/129847908