The knowledge sharing zone -

Use of class loader in Java

This page contains the details about Java's class loader Like - Use of class loader in java, Type of class loaders in Java, How class loader works etc..

What is class loader in Java

  • A class loader is an object that is responsible for loading classes. In Java, We have abstract class ClassLoader in java.lang package for the same.
  • The virtual machine's built-in class loader, called the "bootstrap class loader", does not itself have a parent but may serve as the parent of a ClassLoader instance.
  • All JVMs include at least one class loader that is embedded within the JVM called the primordial (or bootstrap) class loader.
  • Every Class object contains a reference to the ClassLoader that defined it.

Use of class loader in Java

  • A class loader should attempt to locate or generate data that constitutes a definition for the class.
  • We can implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.
    • For example, You can create a network class loader to download class files from a server,  Sample code might look like:
    • class NetworkClassLoader extends ClassLoader {
               String host;
               int port;

               public Class findClass(String name) {
                   byte[] b = loadClassData(name);
                   return defineClass(name, b, 0, b.length);

               private byte[] loadClassData(String name) {
                   // load the class data from the connection
                    . . .

  • Class loaders may typically be used by security managers to indicate security domains.

Type of class loaders in Java

  1. Primordial (Bootstrap) class loader

  2. Non-primordial class loader

    1. Extensions class loader

    2. System class loader

  • Bootstrap (primordial) class loader : Loads JDK internal classes, java.* packages. (as defined in the sun.boot.class.path system property, typically loads rt.jar and i18n.jar)
  • Extensions class loader : Loads jar files from JDK extensions directory (as defined in the java.ext.dirs system property – usually lib/ext directory of the JRE)
  • System class loader : Loads classes from system classpath (as defined by the java.class.path property, which is set by the CLASSPATH environment variable or –classpath or –cp command line options)

Important Notes

  • Classes loaded by Bootstrap class loader have no visibility into classes loaded by its descendants (ie Extensions and Systems class loaders).
  • The classes loaded by system class loader have visibility into classes loaded by its parents (ie Extensions and Bootstrap class loaders).
  • If there were any sibling class loaders they can not see classes loaded by each other.
  • They can only see the classes loaded by their parent class loader.
    • For example Sibling1 class loader cannot see classes loaded by Sibling2 class loader, Both Sibling1 and Sibling2 class loaders have visibilty into classes loaded by their parent class loaders (eg: System, Extensions, and Bootstrap).

How class loader works

  • Class loaders are hierarchical and use a delegation model when loading a class.
  • Class loaders request their parent to load the class first before attempting to load it themselves.
  • When a class loader loads a class, the child class loaders in the hierarchy will never reload the class again.
  • Hence uniqueness is maintained.
  • Classes loaded by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not true.

You would also like to read:

Declare class with modifier or visibility
What is class and object in java
Range of numeric primitive data types in java
Identifiers, Literals and Keywords
What is JavaBean

Comments :

Name :
Email :
Comment :
Verify Text :
capch image, refresh page if not loaded somehow