1. One command corresponds to one process.
When we start a Java program, that is, when we start a main method, a Java virtual machine process will be started, no matter how complex the process is. Different JVM processes will not affect each other. This is why it is said that the Java program has only one entrance-the main method, which is called by the virtual machine. The two main methods correspond to two JVM processes, starting two different class loaders, and actually operating 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 through loading, connection, and initialization.
1. Class loading: refers to reading the class file of the 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 to the static variables of the class and perform default initialization.
c). Parsing: Replace the symbol references in the binary data of the class with direct references.
3. Initialization: Initialize the static variables and static initialization blocks of the class.
(Note: For a static property of final type, if the property value has been obtained at compile time, calling the property will not cause the class to be initialized, because this is equivalent to using a constant;
Using the ClassLoader() method only loads the class and does not initialize it. )
3. Class loader.
The class loader is responsible for loading .class files into memory and generating corresponding java.lang.Class objects. It is responsible for loading all classes. Once a class is loaded into the JVM, it will not be loaded again. Entered.
In Java, a class is identified by its fully qualified class name (that is, package name + class name).
In the JVM, a class is identified by its fully qualified class name and its class loader.
When the JVM is running, three ClassLoaders will be generated, namely: BootstrapClassLoader (root class loader), ExtClassLoader (extended class loader) and AppClassLoader (system class loader). The UML structure is as follows:
Among them, BootstrapClassLoader is responsible for loading the core class library of JRE. It is not a subclass of ClassLoader and is written in C++, so we cannot see it in Java. When obtained through the getParent() method of its subclass, null will be returned. BootstrapClassLoader is responsible for loading Java core class libraries such as rt.jar and charsets.jar under the JRE target.
As can be seen from the figure, ExtClassLoader and AppClassLoader are subclasses of ClassLoader. You can't see them in the API, they are in the rt.jar file. The fully qualified class names are:
sun.misc.Launcher$ExtClassLoader and sun.misc.Launcher$AppClassLoader.
Among them, ExtClassLoader is responsible for loading the JAR package in the JRE extension directory ext, and AppClassLoader is responsible for loading the class package under the Classpath path.
The test is as follows:
Copy the code code as follows:
package com.stopTalking.crazy;
public class TestClassLoader {
public static void main(String[] args) {
//Get the class loader of the current thread
ClassLoader loader = Thread.currentThread().getContextClassLoader();
//Get the class loader of the System class
ClassLoader loader1 = System.class.getClassLoader();
//Get the class loader loader2 of this class TestClassLoader
ClassLoader loader2 = TestClassLoader.class.getClassLoader();
//Get the parent class of loader2
ClassLoader loader3 = loader2.getParent();
//Get the parent class of loader2's parent class
ClassLoader loader4 = loader3.getParent();
System.out.println(loader);
System.out.println(loader1);
System.out.println(loader2);
System.out.println(loader3);
System.out.println(loader4);
}
}
Console output:
Copy the code code as follows:
//The class loader obtained by the current thread class is AppClassLoader
sun.misc.Launcher$AppClassLoader@6b97fd
//The System class is loaded by the root loader and cannot be accessed in java, so it is null
null
//The class loader of this class is of course also AppClassLoader
sun.misc.Launcher$AppClassLoader@6b97fd
sun.misc.Launcher$ExtClassLoader@1c78e57
null