What are the different types of class loaders in Java?
In Java, class loaders are responsible for loading classes into memory during runtime. There are several types of class loaders, each with a different purpose and scope.
Types of Class Loaders
- Bootstrap Class Loader:
- Description: The Bootstrap Class Loader is the parent of all class loaders in the JVM. It loads core Java classes (like
java.lang.*
,java.util.*
) from the JRE’slib
directory orrt.jar
. - Scope: It has access to the most fundamental classes of the Java runtime.
- Functionality: It is implemented in native code and is responsible for loading the core libraries of the JVM.
- Description: The Bootstrap Class Loader is the parent of all class loaders in the JVM. It loads core Java classes (like
- Extension Class Loader:
- Description: The Extension Class Loader loads classes from the extensions directory (
jre/lib/ext
) or from any JAR files specified in thejava.ext.dirs
system property. - Scope: It is responsible for loading classes provided by external libraries or extensions to the Java platform.
- Functionality: This class loader is used to load JAR files that are part of Java extensions.
- Description: The Extension Class Loader loads classes from the extensions directory (
- System Class Loader (Application Class Loader):
- Description: The System Class Loader loads classes from the classpath (the
CLASSPATH
environment variable or any directories or JARs specified in it). - Scope: It is responsible for loading classes for user applications and third-party libraries.
- Functionality: This class loader is the default class loader used to load user-defined classes and libraries.
- Description: The System Class Loader loads classes from the classpath (the
- Custom Class Loaders:
- Description: Developers can create their own class loaders by extending the
java.lang.ClassLoader
class. Custom class loaders are used to load classes in specific ways, such as loading classes from a database, network, or encrypted sources. - Scope: Custom class loaders can be used to implement advanced features like class reloading, dynamic loading, or isolation of different applications.
- Functionality: This allows for more control over the class loading process.
- Description: Developers can create their own class loaders by extending the
Class Loader Hierarchy
Class loaders follow a parent-child relationship, where each class loader can delegate the task of loading a class to its parent class loader before attempting to load it itself. The hierarchy is as follows:
- Bootstrap Class Loader (parent of all)
- Extension Class Loader (child of Bootstrap)
- System Class Loader (child of Extension)
- Custom Class Loaders (children of System Class Loader)
Delegation Model
The class loader uses a delegation model, which works as follows:
- When a class needs to be loaded, the JVM first asks the parent class loader (Bootstrap) to load the class.
- If the parent loader cannot find the class, it will be asked to load it by the next child class loader in the hierarchy, and so on.
Conclusion
Java provides multiple class loaders, each responsible for loading different types of classes. Understanding the different types of class loaders and their responsibilities can help in customizing the class loading process and in optimizing the runtime behavior of Java applications.