Learn reflection with teacher Wang (ii): loading, connecting, and initializing Java classes Teacher: Wang Shaohua QQ Group No.: 483773664 Study content:
Understanding the load, connection, and initialization of classes
The life cycle of a class
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/82/88/wKioL1dYC1DQ65QqAAA5CNqThxg934.png " data_ue_src= "E:\My knowledge\temp\900d2ef6-537c-4188-8321-1e314d3dd03c.png" >
When we write a Java source file, compiled will generate a suffix named class file, which is called bytecode files, only such bytecode files can be run in the Java Virtual machine, Java class life cycle refers to a class file from loading to unloading the entire process. The complete life cycle of a Java class will go through five stages of loading, connecting, initializing, using, and unloading, and of course there is no direct use without being initialized after loading or connecting, here we mainly study the parts that the ClassLoader executes, that is, loading, linking, and initializing.
Second, the class of loading (a) concept
Class is loaded by reading the class file into memory and creating a Java.lang.Class object for it, which means that when any class is used in the program, the system creates a Java.lang.Class object for it.
Note:
As we said before: A class is an abstraction of an object, and a class is something of a conceptual hierarchy. But a class is also an object. Just as we say that concepts are primarily used to define and describe other things, but the concept itself is also a thing, then the concept itself needs to be described.
Each class is an abstraction of a group of objects with the same characteristics, and all of the classes in the system, which are actually objects, are java.lang.Class instances.
(ii) Class loader
The class loader is the object that is responsible for loading the class. The Java Virtual machine provides us with two kinds of loaders:
1, the Java Virtual machine comes with the loader
1) root ClassLoader (written in C + +, programmers cannot get the class in Java code)
2) Extension loader, using Java code to implement
3) System loader (application loader), using Java code to implement
2. User-defined ClassLoader: Developers can create their own class loaders by inheriting the ClassLoader class
The ClassLoader class is an abstract class. If a binary name is given to a class, the ClassLoader attempts to find or generate the data that makes up the class definition. The general strategy is to convert the name to a file name and then read the class file for that name from the file system. Each class object contains a reference to the ClassLoader that defines it.
Let's take a look at a method of class getClassLoader
1 |
public ClassLoader getClassLoader() |
Returns the class loader for this class. If the class is loaded by the root class loader, this method returns Null in such implementations.
Test getClassLoader method
12345678 |
public class ClassLoaderTest {
public static void main(String[] args)
throws ClassNotFoundException {
Class clazz = Class.forName(
"java.lang.String"
);
System.out.println(clazz.getClassLoader());
Class clazz2 = Class.forName(
"chapter09_02.A"
);
System.out.println(clazz2.getClassLoader());
}
}
|
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/89/wKiom1dYCkeioNoIAAAQOdwcEwU426.png " data_ue_src= "E:\My knowledge\temp\6e5ead2f-8301-4a7e-8f89-fd04dffa2591.png" >
(iii) How to load the. class file
1. Load directly from the local system
2. Download the. class file over the network
3. Load. class files from archive files such as Zip,jar
4. Extract the. class file from the proprietary database
5. Dynamically compile the Java source file into a. class file
(iii) The loading process of the class
The end product loaded by the class is a class object that is located in the heap area
The class object encapsulates the data structure of the classes within the method area, and provides the Java programmer with an interface to access the data structures within the method area
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/89/wKiom1dYCkigbhyMAADfwv8SOS8882.png " data_ue_src= "E:\My knowledge\temp\838c0ae7-c260-4c31-bca7-e838d8638e69.png" >
Third, the connection of the class
After the class is loaded, it enters the connection phase. A connection is the merging of binary data from a class that has been read into memory into the runtime environment of the virtual machine. It consists of three stages:
Validation: Ensure that the class being loaded is correct
Prepare: Allocates memory for static variables of a class and initializes it to a default value
Parse: Converts a symbolic reference in a class to a direct reference
Iv. class Initialization (i) concepts
Assigns the correct initial value to the static variable of the class.
In a Java class, there are two ways to specify an initial value for a static property
Specifying an initial value when declaring a static property
To specify an initial value for a static property using a static initialization block
123456789 |
public class A {
//声明时赋值
public static int a =
9
;
public static int b;
//使用静态化块赋值
static
{
b=
12
;
}
}
|
(ii) Interview questions: What is the output of the following code?
123456789 |
public class ClassLoaderTest2 {
static
{
b=
12
;
}
static int b =
9
;
public static void main(String[] args)
throws ClassNotFoundException {
System.out.println(b);
}
}
|
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/88/wKioL1dYC1Gj4ca3AAAURvtIzbQ368.png " data_ue_src= "E:\My knowledge\temp\704dd2a0-dcb5-42be-b499-d036e717ad24.png" >
(iii) steps for the JVM to initialize a class
The JVM Initializes a class that consists of the following steps:
If this class has not been loaded and connected, the program first loads the connection class;
If the immediate parent of the class is not initialized, the immediate parent class is initialized first;
If there are initialization statements in the class, the system executes the initialization statements sequentially.
When performing the second step, the system also follows these three steps for the initialization of the direct parent class, and if the immediate parent class has a direct parent class, the system repeats the three steps again to initialize the parent class first .... In turn. Therefore, when a Java virtual machine Initializes a class, all of its parent classes are required to be initialized.
But this rule does not apply to interfaces.
When a class is initialized, the interface it implements is not initialized first
When an interface is initialized, its parent interface is not initialized first
Therefore, a parent interface is not initialized because of its sub-interfaces or the initialization of the implementation class. the initialization of the interface is only caused when the program first uses a static variable of a particular interface
Summarize
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/88/wKioL1dYC1LBhdzAAABCYzkmMEA309.png " data_ue_src= "E:\My knowledge\temp\92d1a454-2436-4158-8826-596079ac10a8.png" >
From for notes (Wiz)
Learn from Teacher Wang Reflection (ii): loading, joining, and initializing Java classes