Class Loaders in Java

Monali Shinde
3 min readNov 21, 2020


Functionality of class loaders:

ClassLoader is class in java which comes into picture at the time of loading class files. Java code is compiled into a class file by compiler and JVM executes Java program, by executing byte code written in the class file. ClassLoader is responsible for loading class files from file system.

Basically there are three types of class loaders:

1. Bootstrap class loader : Every JVM implementation must have a bootstrap class loader. It loads core java API classes present in lib directory of jre folder of jdk. This path is popularly known as bootstrap path. It is implemented in native languages like C, C++. The bootstrap class loader is also known as Primordial ClassLoader in Java.

2. Extension class loader : It is child of bootstrap class loader. It loads the classes present in the extension directories ext(Extension path) or any other directory specified by the java.ext.dirs system property. It is implemented in java by the sun.misc.Launcher$ExtClassLoader class.

3. System/Application class loader : It is child of extension class loader. It is responsible to load classes from application class path. It internally uses environment variable which mapped to java.class.path. It is also implemented in Java by the sun.misc.Launcher$AppClassLoader class.

4. Custom Class Loaders : JVM has its own class loaders, but they load files from a local file system or hard drive location. Following are the use cases where we need class loaders

  1. Some of the classes in application are getting used for a finite period of time. By using custom class loaders, we can unload these classes after use. In this way it helps in better memory management.
  2. Classes can be loaded from anywhere.
    For example: Databases, Networks, or even the source can be defined on the fly.

The class loader reads the .class file, generate the corresponding binary data and save it in method area. For each .class file, JVM stores following information in method area.

  • Fully qualified name of the loaded class and its immediate parent class.
  • Whether .class file is originated from Class or Interface or Enum
  • Modifiers, Variables and Method information etc.

After loading .class file, JVM creates an object of type Class to represent this file in the heap memory, which is predefined in java.lang package. This object can be used by the programmer for getting class level information like name of class, parent name, methods and variable information etc. To get this object reference we can use getClass() method of Object class.

How Class loader works in Java?

JVM follows Delegation-Hierarchy principle ,Visibility Principle and Uniqueness Principle to load classes.

According to Delegation-Hierarchy principle, System class loader delegates load request to extension class loader and extension class loader delegates request to boot-strap class loader. If class found in boot-strap path, class is loaded otherwise request propagates to extension class loader and then to system class loader. At last if system class loader fails to load class, then we get run-time exception java.lang.ClassNotFoundException

According to the visibility principle, child ClassLoader can see classes loaded by parent but vice-versa is not true. This means if class ABC is loaded by Application class loader then trying to load class ABC explicitly using extension ClassLoader will throw java.lang.ClassNotFoundException

According to Uniquenenss principle, a class loaded by parent should not be loaded by child ClassLoader again. Though it’s completely possible to write a class loader which violates Delegation and Uniqueness principles and loads class by itself, it’s not something which is beneficial. You should follow all class loader principles while writing your own ClassLoader.

Thanks for reading :-)

Helpful links: