The classloader takes three steps to mount a class to a Java Virtual Machine: Loading, connection, and initialization. The connections are divided into verification, preparation, and resolution processes; after the Java source program is compiled, the bytecode is interpreted and executed by the Java virtual machine, and then interpreted by the interpreter of the Java Virtual Machine as the machine code of a specific system.
Reprinted: Find and import the. Class File
Connection: Verify (ensure that the Java data format is correct and used with the Java Virtual Machine), prepare (allocate the required memory for this type), parsing (convert the symbol reference in the constant pool to direct reference)
Initialization: assign appropriate initial values to class variables.
1. Loading:
Description: The Java VM loads the specified class file.
Result: The instance object of this class is formed.
Process: JVM uses the class loader to locate the corresponding class file, then reads the class file (a linear binary data stream), passes it into the Java virtual machine, and then extracts the type information from the virtual machine, for example, the class name, method name, variable name, modifier, and return type of the method. There is also an important constant pool (the constant pool stores all constants of this type, including direct constants and symbolic references to other types, fields, and methods) save the information in a place called the method area. Eventually, a class instance is formed, which is stored in the heap area of the memory. It becomes an interface between a Java program and an internal data structure. To access the information of this type, the program calls the method of the class instance object corresponding to this type. In short: this process is to resolve a type of binary data into an internal data structure in the Method Area and create a class object on the stack.
2. Connection
Description: verification, preparation, and resolution.
Result: This type is correct.
Process:
Verify that the type conforms to the semantics of the Java language. For example, the final class cannot have subclasses and the final method cannot be overwritten, make sure that there is no incompatible method declaration between the type and the super type (for example, the two methods have the same name, the parameters are identical, but the return type is different ).
Preparation: the Java Virtual Machine allocates memory for class variables and sets the default value.
Resolution: searches for classes, interfaces, fields, and methods in the type constant pool. The application replaces these symbol references with the direct application process.
Example:
A global variable is defined in the class: final public static size = 1; user u = new user ();The Java Virtual Machine allocates memory for the size and assigns the default value 0. find the reference to the user class in the constant pool. If the user class is not loaded, load and connect the class, and replace the reference to the user class in the constant pool with a direct reference. At this time, the user class will not be initialized because it is not used yet.
3. Initialization
Description: initializes the static zone, including static variables and static code blocks.
Result: this type can be used.
Process: The () method may be called (this method can only be called by the Java Virtual Machine) to initialize the static variables of this class. Before calling this method, you must confirm that the () method of the class has been called. The Java Virtual Machine assigns the value of size to 1.
4. Use this type of code:
4.1 user u = new user (); (stored in the heap area of the memory)
A user class instance is actually instantiated through the class instance of this class. The method is as follows:
User u = (User) class. forname ("user"). newinstance (); For convenience, use C instead of class. forname ("user ").
4.2 U. setname ("Li Wenshui"); U. setpwd ("159 ");
Call the method of this class and assign values to the variables of this class. The internal call of the Java Virtual Machine is like this. Find this method in the method area and call it using the following method of the class instance:
C. getmethod ("setname"). Invoke (u, "Li Wenshui ");
4.3 string name = U. getname (); string Pwd = U. getpwd ();
Similar to step 2, the difference is that the obtained values are assigned to the variable name and PWD respectively. The key is where this value is stored? Same as the instance object, it is stored in the heap area. At this time, I should be able to see the role of the class instance, which plays an intermediate role and reflects the calls in the program to the changes in the data in the heap.
Source: http://zhidao.baidu.com/question/186854489.html