Detailed life cycle of the class (1)

Class loading:

  • The final product of class loading is an Classobject located in the heap
  • ClassThe object encapsulates the data structure of the class in the method area, and provides an interface to the JAVA programmer to access the data structure in the method area
  • Types of class loaders:
    1. The loader that comes with the java virtual machine
      (1) root class loader (Bootstrap), written in C ++, cannot get the class in java code
      (2) extension class loader (Extension), using Java code to achieve
      (3) system (Or application) class loader (System), using Java code
    2. User-defined class loader
      (1) Subclass of java.lang.ClassLoader
      (2) User can define the loading method of the
      class 3. The class loader does not need to wait for a class to be "first used" before loading it:
      (1) The jvm specification allows class loading to pre-load a class when it is expected to be used. If a .Classfile is missing or there is an error during the pre-loading process , the class loader must only use the class when the program first actively uses the class Report errors (LinkageError errors).
      (2) If a class has not been actively used by the program, the class loader will not report an error.
      (3) After the class is loaded, it will enter the connection phase. The connection is to merge the binary data of the class that has been read into the memory into the runtime environment of the virtual machine.

Class verification:

  • Structure inspection of class files: ensure that class files follow the fixed format of java files
  • Semantic inspection: to ensure that the class itself conforms to the grammar requirements of the Java language, such as verifying that the final type class has no subclasses and the final type method is not overwritten (to prevent malicious operations by malicious users)
  • Bytecode verification: to ensure that the bytecode stream can be safely executed by the java virtual machine. The bytecode stream represents java methods (including static methods and instance methods). It is a sequence of single-byte instructions called opcodes, and each opcode is followed by one or more operands. The bytecode verification step checks whether each opcode is legal, that is, whether it has legal operands.
  • Verification of binary compatibility: to ensure coordination between the mutually referenced classes. For example Worker, the goWork()method of the Carclass will be called in the method of the class run(). When the Java virtual machine verifies the Workerclass, it will check whether Carthe run()method of the class exists in the method area . If it does not exist (when the version of the Workerclass and the Carclass are incompatible, this problem will occur and an NoSuchMethodErrorerror will be thrown )

Class preparation:

In the preparation stage, the java virtual machine allocates memory for the static variables of the class and sets the default initial value. For example, for the following Simpleclass, in the preparation stage, the inttype of static variable is aallocated 4a byte of memory space, and assigned a default value 0. Allocate a byte of memory space for longa static variable of type and assign a default valueb80

public class Simple {

    private static int a=1;
    public static long b;
    static {
        b=2;
    }
}

Class resolution stage:

In the parsing phase, the Java virtual machine replaces the symbolic reference in the binary data of the class with a direct reference, for example Worker, the goWork()method of the Carclass will be referenced in the method of the class run().

 public void goWork(){
        ca.run();//表示符号引用
    }

In Workerincludes a binary data class Carof run()the method of symbolic references, which consists of run()the full class name and associated method descriptor composition. In the resolution phase, the virtual machine directly replaces this symbol reference with a pointer, which points to the memory location Carof the run()method of the class in the method area. This pointer is a direct reference.

Class initialization:

1. In the initialization phase, the java virtual machine executes the class initialization statement and assigns initial values ​​to the static variables of the class. In the program, there are two ways to initialize static variables:

  • Initialize at the declaration of static variables
  • Initialize in a static code block.
    For example, in the following code, both static variables aand bare explicitly initialized, but cnot explicitly initialized, it will keep the default value by default0
public class Simple {

    private static int a=1;
    public static long b;
    public static long c;
    static {
        b=2;
    }

}

2. The steps of class initialization:

  • If this class is still available 没有加载和连接, load and connect first
  • If the class has a direct parent class, and the parent class has not been initialized, then initialize the direct parent class first
  • If there are direct initialization statements in the class, then execute these initialization statements in sequence.

3. Timing of class initialization:
When the java virtual machine initializes a class, all of its parent classes are required to be initialized, but this rule does not apply to interfaces.

  • When initializing a class, it does not first initialize the interface it implements
  • When an interface is initialized, its parent interface will not be initialized first.
    Therefore, a parent interface will not be initialized because of the initialization of its sub-interface or implementation class, only when the program first uses a static variable of a specific interface. Causes the initialization of the interface.

Use phase:

There are still three steps in the use of classes: object instantiation, garbage collection, and object termination.

  • Object instantiation: It is the content of the constructor in the execution class. If there is a parent class in the class, the JVM will first execute the constructor of the parent class by displaying or implicitly, creating space for the instance variables of the parent class in the heap memory Assign the default initial value, and then assign the real value to the instance variable itself according to the content of the constructor code. Then, reference the variable to obtain the first address of the object, and call the instance variable and method by manipulating the object.
  • Garbage collection: When the object is no longer referenced, it will be marked with a special garbage mark by the virtual machine and wait for GC collection in the heap
  • The end of the object: After the object is recycled by the GC, the object no longer exists, and the life of the object comes to an end

Uninstall stage:

That is, the life cycle of the class has reached the last step. There is no longer a reference to the class in the program, and the class will be garbage collected by the JVM, and the life will end ...
Insert picture description here

Published 41 original articles · Liked 14 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/Yunwei_Zheng/article/details/104018013