In Java, class loading follows a delegation model, where each class loader delegates the class loading request to its parent before attempting to load it itself. The three main class loaders in modern Java (JDK 9 and later) are:
1. Bootstrap ClassLoader
- Role:
Loads the core Java classes that are essential for running Java applications, such as classes fromjava.lang
,java.util
, etc. - Source:
Loads from the JDK’s runtime image (since JDK 9, thert.jar
file was removed). - Implemented in:
Native code (C/C++), as part of the JVM. - Parent:
It has no parent, as it is the root class loader.
2. Platform ClassLoader (JDK 9+, formerly Extension ClassLoader)
- Role:
Loads classes from the Java platform modules that are not part of the core JDK, such as:java.sql
java.xml
java.logging
- Source:
Instead ofjre/lib/ext/
(which existed in JDK 8 and earlier), it now loads platform-specific modules. - Implemented in:
Java (part ofjdk.internal.loader.ClassLoaders$PlatformClassLoader
). - Parent:
Bootstrap ClassLoader.
3. Application ClassLoader (System ClassLoader)
- Role:
Loads user-defined classes (e.g., application code and external dependencies). - Source:
Loads classes from the classpath (-cp
orCLASSPATH
environment variable) or module path (--module-path
in JDK 9+). - Implemented in:
Java (part ofjdk.internal.loader.ClassLoaders$AppClassLoader
). - Parent:
Platform ClassLoader.