The relationship between the JVM and the class
When we call the Java command to run a Java program, the command will start a Java Virtual machine process, no matter how complex the Java program is, how many threads the program launches, all of which are in the Java Virtual machine process. As described earlier, all threads of the same JVM, All variables are in the same process, and they all use 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.
L program runs to use System.exit () or Runtime.getruntime (). exit () Code end Program
L End of an uncaught exception or error encountered during program execution
L The program is on the platform to force the end of 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 initialize the class by loading, connecting, initializing steps, and if there is no accident, the JVM will complete the three steps consecutively, so it is sometimes referred to as class loading or class initialization.
Class loading refers to reading class files into memory and creating a Java.lang.Class object for them, which means that when any class is used in a program, the system creates 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 all our programs to run, and these classloader are often referred to as System Class loaders. In addition, developers can create their own class loaders by inheriting the ClassLoader base class.
File loaded by the class loader:
• Loading a class file from the local system
L load class file from Jar package
L Load class files over the network
• Dynamically compile and load a Java source file
Class of connections
The class connection is divided into 3 stages:
Validation: Verifies that the class being loaded has the correct internal structure and is consistent with other classes
Prepare: Allocate memory for static properties and set default initial values
Parsing: Replacing symbolic references in binary data in a class with direct references (replacement of final modified constants)
Initialization of the class
The primary is to initialize a static property
The JVM Initializes a class that consists of the following steps:
If the class has not been loaded and connected, the program loads and connects 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.
When does the class load or class initialize?
L Create an instance of a class (new method)
L call a static method of a class
L Access static properties of a class or interface
L Use the Reflection mechanism to force the creation of a Java.lang.Class object corresponding to a class or interface. (Class.forName ("person"))
Initialize subclasses of a class
L Use the Java.exe command directly to run a main class
Note:
When a class is loaded using the LoadClass method of the ClassLoader class, the method simply loads the class and does not perform initialization of the class. When you use the forname () static method of class, it causes the class to be forced to initialize.
Loading, linking, initializing of Java classes