Basic concepts of Class Loaders:
As the name implies, the ClassLoader (class loader) is used to dynamically load Java classes into a Java virtual machine. This means that when a program needs a class, the ClassLoader adds two of that class to the virtual machine.
The tree-like structure of the ClassLoader
The ClassLoader in Java can be broadly divided into two categories, one for the system, the other for Java application developers. There are three main types of ClassLoader available in the system:
- Boot class loader (bootstrap class loader): It is used to load Java's core library, which is implemented by native code and does not inherit from
java.lang.ClassLoader
.
- Extension class loader (Extensions class loader): It is used to load the Java extension library. The implementation of the Java virtual machine provides an extension library directory. The ClassLoader finds and loads the Java class in this directory.
- System class Loader: It loads Java classes according to the Classpath (CLASSPATH) of the Java application. In general, Java-applied classes are loaded by it. You can
ClassLoader.getSystemClassLoader()
get it by.
All Class loaders have a parent class loader, in addition to the boot class loader. Developers can java.lang.ClassLoader
implement their own ClassLoader by inheriting classes to meet some special requirements.
Code View tree structure:
1 public class Classloadertree {2 3 public static void main (string[] args) {4 ClassLoader loader = C Lassloadertree.class. getClassLoader (); 5 while (loader! = null) { System.out.println (loader.tostring ()); 7 loader = }10}
Operation Result:
[Email protected] [Email protected]
Classloadertree is the system loader, which is an instance class of the Appclassloader class.
Note: There is no input to the Bootstrap class loader because it is written inside the system by the C language, so there is no access to the Java program.
The process of loading a class
The ClassLoader load class uses the overall responsibility of the delegation mechanism. The so-called overall responsibility, that is, when a classloader loading a class, the class depends on the and reference of all classes are also loaded by this classloader, unless explicitly using another ClassLoader loading The delegate mechanism is to look for the parent class loader (instead of super, which is not an inheritance relationship with the parent ClassLoader Class), only to find it from its own classpath when the parent cannot find it. In addition, the class loading also uses the cache mechanism, that is, if the cache saved this class directly return it, if not only to read from the file and converted to class, and stored in the cache, this is why we modified the class but must restart the JVM to take effect.
Java class Loader