Home >Java >javaTutorial >What is the process of jvm class loading?
#The process of jvm class loading is divided into three steps: loading, connection and initialization.
Loading
Load this class from the outside into the jvm through the full path of the class, and at the same time in the method area Generate the description information of the class and generate the Claas type of the class in memory. As the data access entry of this class in the method area.
Connection
This is a very important step. The process is more complicated and is divided into three steps: verification, preparation and analysis.
Verification: Ensure the correctness of class loading. Generally speaking, there will be no problem with class files compiled by javac, but some people's class files may be compiled by themselves through other methods, which may not comply with the JVM compilation rules. This step is to filter out this part. Illegal file
Preparation: Allocate memory for static variables of the class and initialize them to default values. We all know that static variables can be assigned values manually without us. They will naturally have an initial value. For example, the initial value of the int type is 0; the initial value of the boolean type is false, and the initial value of the reference type is null. Note here that only memory is allocated for static variables, and there is no object instance at this time.
Analysis: Convert symbol references in the class into direct references. Explain symbolic references and direct references. For example, when using method B in method A, A(){B();}, B() here is a symbolic reference. When we first learned Java, we all knew that this was a reference to java and thought that B pointed to the memory address of method B. But this is incomplete. The B here is just a symbolic reference. It does not have much practical significance for calling the method. It can be thought of as a sign for the programmer to let the programmer know that this method can be like this Call, but when method B is actually called, it points to the memory address of method B through a pointer. This pointer is really responsible for the method call, and it is a direct reference.
Initialization
In the initialization phase, the initialization method clinit() of the class will be called to assign actual values to static variables (for example, assign value to 123) and execute static code piece. There is no mandatory constraint on the timing of loading in the JVM specification. However, for initialization, the JVM specification strictly stipulates that there are only 5 situations in which a class must be initialized immediately:
Let's explain how the clinit() method is generated. of. The clinit() method is generated by the compiler automatically collecting static variables and static statements in the class. The order in which the compiler collects is determined by the order in which statements appear. A static statement block can only copy variables defined after it, but cannot use them, as shown in the figure below, and the virtual machine specification guarantees that the clinit() method of the parent class Must be executed before subclasses, but not through inheritance.
The above is the detailed content of What is the process of jvm class loading?. For more information, please follow other related articles on the PHP Chinese website!