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.jarfile 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.sqljava.xmljava.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 (-cporCLASSPATHenvironment variable) or module path (--module-pathin JDK 9+). - Implemented in:
Java (part ofjdk.internal.loader.ClassLoaders$AppClassLoader). - Parent:
Platform ClassLoader.