I. class loaders
- The Class Loader loads the. Class file into the bytecode of the Java Virtual Machine.
- Multiple class loaders can be installed in the Java Virtual Machine. By default, the system has three main class loaders. Each class is responsible for loading classes at specific locations: Bootstrap, extclassloader, and appclassloader.
- The classloader is also a Java class, because the classloaders of other Java classes are also loaded by the classloaders. Obviously, the first classloaders must be not Java classes, this is exactly Bootstrap.
- All class loaders in the Java Virtual Machine are organized in a tree structure with parent-child relationship. when instantiating each class loader object, you need to specify a parent class loader object for it or use the system class loader as its parent class by default.
Ii. parent-child relationship between class loaders and rule Dimensions 3. Delegation mechanism of class loaders which class loaders are dispatched to load a class when the Java Virtual Machine loads a class?
- First, the class loader of the current thread loads the first class in the thread.
- If Class A references Class B, the Java virtual machine uses the Class Loader loaded Class A to load Class B.
- You can also directly call the classloader. loadclass () method to specify a class loader to load a class.
When each class loader loads a class, it first delegates it to its parent class loader.
- When all the ancestor class loaders are not loaded to the class, return to the initiator class loader and cannot be loaded, the classnotfoundexception is thrown, instead of finding the son of the initiator class loader, because there is no getchild method, even if you have multiple sons, which one can you find?
- Explain the reason why extclassloader is running after the classloadertest is output to the itcast. jar package in the JRE/lib/EXT directory.
public class classloadertest {public static void main (string [] ARGs) {// nullpointerexception occurs, because this class is loaded by the bootstrap loader, it is not a Java class // system. out. println (system. class. getclassloader (). getclass (). getname (); // change to the following output: NULL system. out. println (system. class. getclassloader (); system. out. println (classloadertest. class. getclassloader (). getclass (). getname (); classloader loader = clas Sloadertest. Class. getclassloader (); While (Loader! = NULL) {system. out. println (loader. getclass (). getname (); loader = loader. getparent ();} system. out. println (loader) ;}}