Class Loaders in Java

As it was mentioned above, class loaders are part of the Java Runtime Environment. When JVM requests a class, the class loader tries to locate the class and load the class definition into the runtime using the Fully Qualified Class Name (FQCN). Principles of functionality of a Java ClassLoader are shown in the scheme below :

Principles of functionality are the set of rules or features on which a Java ClassLoader works. There are three principles of functionality, they are:

  1. Delegation Model : the Java Virtual Machine and the Java ClassLoader use an algorithm called the Delegation Hierarchy Algorithm to load classes into the Java file. The ClassLoader works based on a set of operations given by the delegation model. They are : 
  • ClassLoader always follows the Delegation Hierarchy Principle
  • Whenever JVM comes across a class, it checks whether that class is already loaded or not
  • If the class is already loaded in the method area then the JVM proceeds with execution
  • If the class is not present in the method area then the JVM asks the Java ClassLoader Sub-System to load that particular class, then ClassLoader Sub-System hands over the control to Application ClassLoader
  • Application ClassLoader then delegates the request to Extension ClassLoader and the Extension ClassLoader in turn delegates the request to BootStrap ClassLoader
  • BootStrap ClassLoader will search in the BootStrap classpath ($JAVA_HOME/jre/lib). If the class is available then it is loaded, if not the request is delegated to Extension ClassLoader
  • Extension ClassLoader searches for the class in the Extension classpath ($JAVA_HOME/jre/lib/ext). If the class is available then it is loaded, if not the request is delegated to the Application ClassLoader
  • Application ClassLoader searches for the class in the Application classpath. If the class is available then it is loaded, if not then a ClassNotFoundException exception is generated

Another (simplified) scheme of class loading :

2. Visibility Principle : the Visibility Principle states that a class loaded by a parent ClassLoader is visible to the child ClassLoaders but a class loaded by a child ClassLoader is not visible to the parent ClassLoaders

3. Uniqueness Property : the Uniqueness Property ensures that the classes are unique and there is no repetition of classes. This also ensures that the classes loaded by parent ClassLoaders are not loaded by the child ClassLoaders. If the parent class loader isn’t able to find the class, only then the current instance would attempt to do so itself.

Previous Next

Leave a Reply

Your email address will not be published. Required fields are marked *