JVM and Class
When you run a Java program by calling the Java command, the command launches a Java Virtual machine process, regardless of how many threads the Java program starts, how many variables are created, and which are in the Java Virtual machine process, sharing the memory area of the JVM process. The JVM process is terminated when the system has the following conditions:
- The program runs to the last normal end;
- The program runs to the end of the program using the System.exit () or Runtime.getRuntime.exit () code;
- An uncaught exception or error is encountered while the program is running;
- The platform on which the program is located forces an end to the JVM process.
Loading of classes
When the program actively uses a class, if the class has not yet been loaded into memory, the system will load, connect, initialize the three steps to initialize the class (if there is no accident, the JVM will continue to complete these three steps, it is sometimes referred to as the three steps of the class loading or initialization).
A class is loaded by reading the class file into memory and creating a Java.lang.Class object for it (note that it is not an object of the target class). This means that when you use any class in your program, you create a Java.lang.Class object for it.
Classes are loaded by the ClassLoader, which is typically provided by the JVM, which is also the basis for Java programs to run, and these classloader are often referred to as the System class loaders. In addition, developers can create their own class loaders by inheriting the ClassLoader base class.
By using different ClassLoader, you can load the binary data of a class from different sources, usually with the following data sources:
- Loads the class file from the local file system;
- Load the class file from the jar package;
- Load class files over the network;
- Performs a dynamic compilation of a Java source file and executes the load.
Java typically does not have to wait until the class is first used, and the Java Virtual machine allows the system to preload some classes.
Class of connections
When the class is loaded, the system generates a corresponding class object for it, and then enters the class's connection phase.
The connection phase of the class is responsible for merging the class's binary data into the JRE. Class connection can be divided into the following three stages:
- Validation: The validation phase is used to verify that the class being loaded has the correct internal structure and is consistent with other classes;
- Preparation: The preparation phase of the class is responsible for allocating memory for the static properties of the class and setting default initial values;
- Parse: Replaces a symbolic reference in the binary data of a class with a direct reference. (about symbolic references and direct references I think it's very clear here.)
Initialization of the class
During the initialization phase of a class, the virtual machine is responsible for initializing the class, primarily initializing the static property. There are two ways to initialize a static property in a Java class:
- Specifies the initial value when declaring a static property;
- Use static initialization blocks to specify an initial value for a static property.
When initialized, the JVM performs the initialization sequentially in the order in which the statements are arranged in the program. As shown in the following code, the value of end B is 9.
Package com.zhyea.test; Public class Test { static{ b=6; } Static int a = 0; Static int b = 9;}
The JVM includes the following steps when initializing a class:
- If the class has not been loaded and connected, the program executes the load and joins the class first;
- If the immediate parent class of the class has not been initialized, the immediate parent class is initialized first;
- If there are initialization statements in the class, the system executes the initialization statements sequentially.
As you can see from the previous step, when the program actively uses any one class, the system guarantees that the class and all the parent classes will be initialized.
Class initialization time
The time the system starts initializing classes or interfaces includes 6 scenarios:
- Create an instance of the class;
- Call a static method of a class;
- Accesses a static property of a class or interface, or assigns a value to the static property;
- Create a Java.lang.Class object for a class or interface by means of reflection, such as using Class.forName ("person");
- Initializes a subclass of a class. When the subclass is initialized, all the parent classes are initialized;
- When you run a class directly using Java commands.
It is necessary to mention that the static property of the final adornment, such as the final decorated static property, gets the value of the property at compile time, then the static property will be treated as if the constant is not initialized (what does the class compiler do, it needs to be considered). As in the following scenario:
Final Static int MON = 1;
The final decorated static property could not get the property value at compile time, then it would be initialized, as in the following case:
Final Static int TUE = +;
It is also worth mentioning that ClassLoader's LoadClass method does not perform class initialization, but only performs class loading.
Loading, joining, and initializing of Java classes