Java class loader and class reflection usage example

1、 A command corresponds to a process.

When we start a java program, that is, when we start a main method, we will start a Java virtual machine process, no matter how complex the process is. Different JVM processes will not affect each other. This is why the Java program has only one entry - the main method, which is called by the virtual machine. The two Mian methods, corresponding to two JVM processes, start two different class loaders, and actually operate different classes. Therefore, they will not affect each other.

2、 Class loading.

When we use a class, if the class has not been loaded into memory, the system will initialize the class by loading, connecting and initializing.

1. Class loading: refers to reading the class file of a class into the JVM and creating a class object for it.

2. Class connection: refers to merging the binary data of the class into the JRE, which is divided into three stages:

a) . verification: check the correctness of the loaded class file data.

b) . preparation: allocate storage space for static variables of the class and initialize by default.

c) . parsing: replace the symbol reference in the binary data of the class with a direct reference.

3. Initialization: initializes static variables and static initialization blocks of a class.

(Note: for a static attribute of final type, if the attribute value has been obtained during compilation, calling the attribute will not cause the class initialization, because this is equivalent to using a constant;

The classloader () method is used to load the class without initialization.)

3、 Class loader.

The class loader is responsible for The class file is loaded into memory and the corresponding Java. Java. Class file is generated for it Lang. class object, which is responsible for loading all classes. Once a class is loaded into the JVM, it will not be loaded again.

In Java, a class is identified by its fully qualified class name (i.e. package name + class name).

In the JVM, a class is identified by its fully qualified class name and its class loader.

When the JVM runs, three classloaders will be generated: bootstrap classloader (root class loader), extclassloader (extension class loader) and appclassloader (system class loader). The UML structure is as follows:

Among them, bootstrap classloader is responsible for loading the core class library of JRE. It is not a subclass of classloader and is written in C + +. Therefore, we can't see it in Java. When obtained through the GetParent () method of its subclass, it will return null. Bootstrap classloader is responsible for loading rt.jar and charsets.jar under JRE target Jar and other Java core class libraries.

As shown in the figure, extclassloader and appclassloader are subclasses of classloader. They are not visible in the API, they are located in the rt.jar file. Fully qualified class names are:

sun. misc. Launcher $extclassloader and sun misc. Launcher$AppClassLoader.

Among them, extclassloader is responsible for loading jar packages in JRE extension directory ext, and appclassloader is responsible for loading class packages under classpath path.

The tests are as follows:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>