The virtual machine loads the data of the description class from the class file into memory, verifies the data, parses and initializes it, and finally forms the Java type that can be used directly by the virtual machine, which is the class loading mechanism of the virtual machine. In the Java language, the loading, joining, and initialization of types are done during the program's run time.
the life cycle of a class :
Class starts from being loaded into the virtual machine's memory and unloads out of memory, and its entire lifecycle includes: Load (Loading), validate (verification), prepare (preparation), Parse (Resolution), Initialization (initiallization), use (using), and unload (unloading) are 7 stages. Where the validation, preparation, parsing of 3 parts collectively referred to as the connection (linking), these seven stages of the order of occurrence such as:
There are only four cases in which the class must be "initialized" (called an active reference to a Class):
- When encountering the four bytecode directives of new, getstatic, Putstatic, Invokestatic (when using new to instantiate an object, reading or setting a static field of a class, invoking a static method of a Class).
- When you use the Java.lang.reflet package method to make a reflection call to a class.
- When initializing a class, it is necessary to first trigger the initialization of its parent class if it finds that its negative class has not been initialized.
- The virtual machine initializes the main class (the class that contains the main method) when the VM is started.
The process of class loading
Load
Loading is the first stage of a class load. There are two kinds of time-of-Chance trigger class loading:
1. Pre-load. When the virtual machine starts up, it loads the. class file under Rt.jar in the java_home/lib/, which is very often used when the program is running, such as java.lang.*, java.util.*, java.io.* and so on, so as the virtual machine loads together. To prove this is simple, write an empty main function, set the virtual machine parameter to "-xx:+traceclassloading" to get the class load information, run it:
1 [Opened E:\MyEclipse10\Common\binary\com.sun.java.jdk.win32.x86_64_1.6.0.013\jre\lib\rt.jar] 2 [Loaded Java.lang.Object from E:\MyEclipse10\Common\binary\com.sun.java.jdk.win32.x86_64_1.6.0.013\jre\lib\rt.jar] 3 [ Loaded java.io.Serializable from E:\MyEclipse10\Common\binary\com.sun.java.jdk.win32.x86_64_1.6.0.013\jre\lib\ Rt.jar] 4 [Loaded java.lang.Comparable from E:\MyEclipse10\Common\binary\com.sun.java.jdk.win32.x86_64_1.6.0.013\ Jre\lib\rt.jar] 5 [Loaded java.lang.CharSequence from E:\MyEclipse10\Common\binary\com.sun.java.jdk.win32.x86_64_ 1.6.0.013\jre\lib\rt.jar] 6 [Loaded java.lang.String from E:\MyEclipse10\Common\binary\com.sun.java.jdk.win32.x86_ 64_1.6.0.013\jre\lib\rt.jar] 7 [Loaded java.lang.reflect.GenericDeclaration from E:\MyEclipse10\Common\binary\ Com.sun.java.jdk.win32.x86_64_1.6.0.013\jre\lib\rt.jar] 8 [Loaded Java.lang.reflect.Type from E:\MyEclipse10\Common \binary\com.sun.java.jdk.win32.x86_64_1.6.0.013\jre\lib\rt.jar] 9 [Loaded java.lang.reflect.AnnotatedElementFrom E:\myeclipse10\common\binary\com.sun.java.jdk.win32.x86_64_1.6.0.013\jre\lib\rt.jar]10 [Loaded Java.lang.Class from E:\myeclipse10\common\binary\com.sun.java.jdk.win32.x86_64_1.6.0.013\jre\lib\rt.jar]11 [ Loaded java.lang.Cloneable from E:\MyEclipse10\Common\binary\com.sun.java.jdk.win32.x86_64_1.6.0.013\jre\lib\ Rt.jar]12 ...
2. Load at runtime. When a. class file is used by a virtual machine, the. class file is checked in memory to see if it is loaded, and if not, the class is loaded by the fully qualified name of the class.
During the loading phase of the class loading process, the virtual machine needs to complete the following 3 things:
(1) To obtain a binary byte stream that defines this class by using the fully qualified name of a class
(2) Transform the static storage structure represented by this byte stream into the runtime data structure of the method area
(3) Generate a Java.lang.Class object representing this class in memory as the access entry for various data of this class in the method area.
The requirements for these three points are not specific to the virtual machine specification, so the flexibility of the virtual machine implementation and the specific application is quite large. For example, in the first article, there is no indication of where the binary stream will come from or how it should be, so this alone can produce many tricks:
· Obtained from the ZIP package, which is the basis for future jar, ear, and war formats
· Get from the network, the typical application is the applet
· Run-time compute generation, typical application is dynamic agent technology
· Generated by other files, the typical application is JSP, which is generated by the JSP corresponding. class file
· Read from the database, this scenario is relatively rare
In a nutshell, this is part of the most manageable phase of a developer's process of loading the class.
When the load phase is complete, the binary byte stream outside the virtual machine is stored in the method area in the format required by the virtual machine. And then instantiate in memory
Java Virtual machine-class loading