The ClassLoader is used to load the. class file into the virtual machine and generate the Java.lang.Class object.
Class loader structure
The class loaders that the JVM comes with are:
Bootstrap Loader
boot loader aka root loader, is the NU WA level loader, Rt.jar class are loaded by it, such as Java.lang.Object, java.lang.String, Java.lang.Class, Java.lang.ClassLoader and so on. No, it doesn't even have Java to play, but it's not written by Java (if Java is written it's weird, who loads it?). )。 Because it is not a Java object, Object.class.getClassLoader () gets null.
Extension Loader
The extension loader loads the JRE expansion pack, and the directory is: Jre/lib/ext.
System Loader
The system loader, also known as the application loader (because it specifically loads the class in the application), is responsible for loading the environment variable ClassPass the specified directory, in fact the environment variable ClassPass is generally ".", which is the current directory, so the class in the application is loaded by the system loader by default.
Custom Class Loaders
For example:
Public class testclassloader extends ClassLoader{ @Override protectedClass<?>Findclass(String name)throwsclassnotfoundexception {Try{String Filepath=system.getproperty ("User.dir") +file.separator+"src"+file.separator+name.replace (".", File.separator) +". Class";byte[] bytes = Files.readallbytes (NewFile (FilePath). Topath ());returnDefineClass (name, Bytes,0, bytes.length); }Catch(IOException e) {Throw NewClassNotFoundException (name,e); } } Public Static void Main(string[] args)throwsException {Testclassloader ClassLoader =NewTestclassloader (); class<?> Class1 = Classloader.loadclass ("Test. Test "); Test test = (test) class1.newinstance (); }}
Inheritance relationship
Custom loader > Inheritance > System loader > Inheritance > Extension loader > Inherit > bootloader.
The loader's parent loader can be obtained through classloader.getparent (), and classloader.getparent () returns null if the parent loader of the ClassLoader is the bootloader.
The loader's inheritance relationship is not a Java class inheritance relationship, the custom loader and the system loader and the extension loader are all inherited from ClassLoader, so there is no class inheriting the parent-child relationship on the institution, and from the class-inheriting authority, they are fraternal relationships.
The ClassLoader has three major mechanisms: delegate
The parent loader is loaded by the parent loader.
Visibility of
1. Classes loaded by the subclass loader can access classes loaded by the parent ClassLoader.
2. Classes loaded by the parent ClassLoader cannot access classes loaded by the child ClassLoader.
3. The two loader classes that do not have an inheritance relationship cannot access each other.
Single-Sex
Load only once, not repeatedly.
Introduction to the Java class loader