1. Class Loader
- The ClassLoader is used when the program is running to use a class, and the class loader loads the bytecode of that class into memory for execution.
- Java Virtual machines can install multiple classloader, the system defaults to three main class loaders, each class loader is responsible for loading classes in different locations
- Bootstrap,extclassloader,appclassloader.
- Some ClassLoader are also Java classes , so there must be a non-Java class loader to load other Java class ClassLoader, this is boostrap.
- All ClassLoader in a Java Virtual machine are organized in a tree structure with a parent-child relationship . Each instantiation of a class loader object must be specified for it
- A parent class loader object, or the system default class loader as the parent.
2.ClassLoader
Construction Method:
ClassLoader ()/////Use the getSystemClassLoader()
ClassLoader returned by the method to create a new class loader that will act as the parent ClassLoader.
ClassLoader (ClassLoader parent);//Specifies the Parents class loader, the parent class may eventually call Bootstrap as the last parent.
Member Methods:
ClassLoader getParent ();//returns the parent class loader for the delegate.
Static ClassLoader Getsystemclassloader ();//Return to the System class loader
Class<?> loadclass (String name);//Use the specified binary name to load the class.
3. The relationship of three types of loaders and the scope of the load class
BootStrap: Common Java classes, such as collection classes under System,util, and so on.
Extclassloader: We can export our custom classes to the folder of the ClassLoader,
4. Delegate mechanism of class loader
When the class loader loads the class, it delegates the parent loader to find the class and loads the class, and the parent class is then delegated to the parent class until the ancestor loads the class .
Ancestors did not load into the class, will let the next level to find, until the original delegate loader . If you don't, you're going to report an anomaly. classnotfoundexception
- The class loader () of the current thread is first
getContextClassLoader()
loaded to load the first class in a thread.
- If Class A refers to a class B,java, the virtual machine uses the class loader that loads class A to load Class B.
- You can also call the Classloader.loadclass () method directly to specify a class loader to load a class.
java-class Loader