Java object creation process

Determine whether to load, allocate memory (pointer collision or free linked list), initialize to zero, set the object header (which class the instance is, the meta information location of the class, GC generation age, etc.), init method.

Creating an object for the Java virtual machine consists of the following steps:

    1. Allocate memory for objects;
    1. Automatically initialize an instance variable of an object to the default value of its variable type;
    1. Initializes the object and assigns the correct initial value to the instance variable.

For the third step, the JVM can initialize an object in one of three ways, depending on how the object is created:

    1. If the object is created using the clone() method, the JVM copies the value of the instance variable of the cloned object into the new object.
    1. If the object is created through the ObjectInputStream class’s readObject() method, then the JVM initializes non-transient instance variables with serialized data read from the input stream.
    1. If the instance variable is explicitly initialized at declaration time, the initialization value is assigned to the instance variable and the constructor is executed. This is the most common way to initialize an object.

To summarize the object creation process:

  1. When an object is first created and static methods/fields in a class are accessed for the first time, the Java interpreter must first look in the classpath to locate the.class file.
  2. Then.class is loaded (which creates a class object), and all the actions related to static initialization are performed. Therefore, static initialization occurs only once when the Class object is first loaded;
  3. When you create an object using the new method, you first allocate enough storage space for the object on the heap.
  4. The storage space is zeroed out, which automatically sets all primitives in the object to default values (0 for numbers, Boolean and STR), and references to null.
  5. Performs all initialization actions that occur at field definitions (for non-static objects);
  6. Execute the constructor.

The init method

Java generates init methods in the bytecode file after compilation, which are called instance constructors. The instance constructors will converge to init methods such as statement blocks, variable initializations, and the constructor calling the parent class, in the order of convergence:

  1. Parent class variable initialization
  2. Superclass statement block
  3. Superclass constructor
  4. Class variable initialization
  5. Subclass block
  6. Subclass constructor
  • Converging to the init method means putting these operations into init to perform.

Clinit method

Java generates clinit methods, called class constructors, in bytecode files after compilation. The class constructor, like the instance constructor, also initializes static statement blocks, static variables, and converges to the Clinit method in the following order:

  1. Parent static variable initialization
  2. Superclass static statement block
  3. Subclass static variable initialization
  4. Subclass static statement block
  • If the parent class is an interface, the clinit method of the parent class is not called. A class can do without clinit methods.

  • The Clinit method is executed during class loading, whereas init is executed during object instantiation, so Clinit must be executed before init. The whole order is:

  1. Parent static variable initialization
  2. Superclass static statement block
  3. Subclass static variable initialization
  4. Subclass static statement block
  5. Parent class variable initialization
  6. Superclass statement block
  7. Superclass constructor
  8. Class variable initialization
  9. Subclass block
  10. Subclass constructor