Introduction to the

Describes the life cycle of Java classes

Class life cycle

The life cycle of a class looks like this:

  • 1. Load: Find the byte stream and create classes based on it
  • 2. Linking: Merge the created classes into the Java virtual machine for execution
    • 1. Verify whether constraints are met
    • 2. Preparation: Allocate memory for static fields of the loaded class. Create a match reference
    • 3. Parse: Parse conforming references into actual references; If a symbol references a field or method that points to a class that has not been loaded, the class is loaded (not necessarily linked and initialized)
  • 3. Initialization: Assign values to fields marked as constants and execute methods
  • Use 4.
  • 6. Uninstall

As shown above, the life cycle of a class can be broken down into five big steps

The first step of the loading process also involves the class loader, which is also important, but I won’t discuss it here. I’ll discuss it in another article

Static assignment at initialization time

The third step of initialization is important, which involves static code and field execution. During initialization, static assignment can occur in the following two ways:

1. If it is a basic type or string and is final, it is marked as a constant value and initialized directly by the Java Virtual machine

2. Other copy operations and static code blocks are placed in the same method, named as, and locked to ensure that they are executed only once

Initialization trigger

There are roughly the following: primary, New, two-static, child, default, launch, methodHandler

  • 1. When the VM starts, initialize the primary class specified by the user
  • 2. Initialize the target class of the new instruction when a new instruction is encountered to create an instance of the target class
  • 3. When an instruction calls a static method, initialize the class of the static method
  • 4. When an instruction is encountered that accesses a static field, initialize the class in which the static field resides
  • 5. Initialization of a subclass triggers initialization of its parent class
  • 6. If an interface defines the default method, the initialization of the class that implements the interface directly or indirectly triggers the initialization of the interface
  • 7. Initialize a class when a reflection call is made to the class using the reflection API
  • 8. When you first call a MethodHandle instance, initialize the class to which the method points
Will not initialize (may be loaded)

There are the following situations:

    1. A reference to a static field of a parent class by a subclass triggers initialization of the parent class, not the subclass
    1. Defining an array of objects does not trigger initialization of the class
    1. Constants are stored in the constant pool of the calling class at compile time. There is no direct reference to the class in which the constant is defined, and the class in which the constant is defined is not triggered
    1. Getting a Class object by its name does not trigger Class initialization. Hello. Class does not initialize the Hello Class
    1. If initialize is false, Class initialization is not triggered when loading a Class using class.forname. This parameter tells the vm whether to initialize the Class. Class.forname (” JVM.hello “) loads the Hello Class by default
    1. Initialization is also not triggered by the default loadClass method of ClassLoader (loaded, but not initialized).

conclusion

This section describes the life cycle of a class and what you need to know about the initialization steps