DEV Community

DevCorner2
DevCorner2

Posted on

Understanding Class Loaders in Java

In Java, class loaders are responsible for dynamically loading classes into the Java Virtual Machine (JVM) at runtime. There are three primary types of class loaders:


1. Bootstrap ClassLoader

Description:
This is the parent of all class loaders and is part of the JVM itself. It is written in native code and is responsible for loading core Java classes (e.g., classes from the java.lang package or rt.jar in older versions of Java).

Scope:
Loads classes from the Java standard library (e.g., java.util, java.io).

Example:
Classes like java.lang.String are loaded by the Bootstrap ClassLoader.


2. Extension ClassLoader (Platform ClassLoader in Java 9+)

Description:
This class loader loads classes from the extension directories or the ext folder (e.g., jre/lib/ext in older versions). Starting from Java 9, it is referred to as the Platform ClassLoader and loads platform-specific classes.

Scope:
Loads classes that extend the core Java functionality, such as optional packages or APIs.

Example:
Classes from javax.* packages or custom libraries placed in the extension directory.


3. Application ClassLoader (System ClassLoader)

Description:
This is the default class loader for user-defined classes. It loads classes from the classpath specified when running the application (e.g., directories, .jar files).

Scope:
Loads application-level classes and resources.

Example:
Classes from your project or external libraries added to the classpath.


ClassLoader Hierarchy

The class loaders follow a parent-delegation model, where each class loader delegates the loading request to its parent before attempting to load the class itself. This ensures that core Java classes are loaded by the Bootstrap ClassLoader and not overridden by user-defined classes.


Top comments (0)