In the previous article, there are a few introduced a class through the reflection of a loading process, as a developer of Java, for class loading mechanism or some understanding, first ask a few questions:
Loading classes in Java who will do the work. What the load process includes. Class can load, that object. 1, Java Load class work who to do.
Java Virtual machine, which loads the static bytecode file. class into memory. 2. What does the loading process consist of?
Class can also be called the lifecycle of a class, including loading (Loading), validation (verification), preparation (preparation), parsing (resolution), initialization (initialization), use (using) and unloading (unloading) 7 stages. The preparation, validation and resolution of 3 parts are collectively referred to as connections (linking).
Load: Loads the. class file into memory, gets the binary byte stream that defines this class, and creates a class object in the method area.
Connectivity (validation, preparation, resolution): Verifying "important but not necessary": Ensure that content in the. class file meets the requirements of the current virtual machine, including (file format verification, metadata validation, bytecode verification, symbolic reference verification) Prepare: Formally allocate memory for class variable (static) and set the phase of class variable initial value. Example: public static int value=1 with an initial value of 0, since no Java method has been executed at this time, and the put static instruction that assigns value to 1 is compiled and stored in the class constructor () method; So the action to assign the value to 1 will be performed at the initialization stage, but if it is final decorated, the initial value of the preparation phase is 1, because the variable becomes a constant. Resolution: A virtual machine replaces a symbolic reference within a constant pool with a direct reference procedure.
Initialization: The compiler automatically collects all the assignment actions of all class variables in the class and the statements in the static statement block static{} are merged, that is, after the preparation phase, the true static variables are assigned.
the trigger condition for initialization.
(1) Creating an instance of a class, which is the way of new
(2) Accessing a static variable of a class or interface, or assigning a value to the static variable
(3) Calling a static method of a class
(4) Reflection (e.g. Class.forName ("Com.shengsiyuan.Test"))
(5) Initializes a subclass of a class, the parent class is also initialized
(6) A class (Java Test) that is marked as a startup class when the Java virtual machine starts up, running a main class directly using the Java.exe command
3, the class can load, that object.
Objects are created from classes, so the life cycle of an object is contained within the life cycle of the class:
The creation of the object to the recycle is performed during the use of the class
Summary: Class loading process is a process of loading the bytecode files in the hard disk into memory, and assigning a value to class variables to the class being used and then to the class being unloaded from memory.
Reference
https://blog.csdn.net/lixingtao0520/article/details/72927775
Tips:
Need to know ahead of time the JVM memory model, some of the classes and methods in java.lang.*, such as ClassLoader.