Understanding Class Loaders in JAVA

 

Class Loaders

    
The purpose of a Class Loader is to service the request for a class. When, JVM needs a class, it requests the Class Loader for the class by specifying fully qualified name. All JVM's include at least one embedded class loader called the primordial or bootstrap class loader. Class loaders are hierarchical. Classes are loaded into the JVM on demand as they are referenced by name in a class that is already running in the JVM. The very first class is loaded through static main method.

 
When the JVM is started, three class loaders are used

    

Bootstrap (primordial)

Bootstrap class loader written in native code, loads internal core Java libraries. Like java.* located in the <JAVA_HOME>/jre/lib directory )-> Extensions (Loads jar files from JDK extensions directory <JAVA_HOME>/jre/lib/ext)->

System

System class loader loads classes from system classpath defined using -classpath or -cp

    
 

High Level Class Loader flow:

  • Class loaders are hierarchical and use delegation to load the classes.
  • Class loaders request parent to load the class first before attempting to load it themselves. 
  • When a class loader loads a class, the child class loader will never load the class again. Uniqueness is maintained. 
  • Classes loaded by Child has visibility into the classes loaded by the parent but parent doesn't have visibility into classes loaded by parent class loader.

Web Analytics