Java class loading is divided into the following three steps
- Load the .class file into memory
- Bytecode verification, Class data structure analysis, memory allocation
- Static attributes and initial assignment, execution of static blocks
Note: When loading class files, only class variables, static initialization blocks, and static member variables are initialized. The methods in the class will only be loaded into the heap and will not be initialized.
Ways to load classes
Java uses ClassLoader to load classes into memory. Java has the following three ClassLoader
- Bootstrap ClassLoader
This is the top classloader and mainly loads the classes required by the JVM for its own work - ExtClassLoader
this user loads the class under the path of "java.ext.dirs" - AppClassLoader
is an ordinary classloader used to load classes in the classpath path
Java ClassLoader is to ensure that each class can only be loaded once by the level loading mechanism. When each ClassLoader loads a class, it will first call the superior ClassLoader to determine whether it has been loaded. If it is not loaded, and there should be no superior classLoader loaded, then this class will be loaded into the JVM by the current ClassLoader.
Note: jvm uses the full class name of Class + ClassLoader instance to load this class to determine whether this class already exists. In other words, the same class is loaded by different ClassLoader instances (different instances of the same ClassLoader class), and the JVM will think that these are two different classes.