The Class Loader in Java is a part of the Java Runtime Environment (JRE) that dynamically loads Java classes into memory when they are required. It allows Java programs to run without knowing all classes at compile time.
.class files into JVM memoryClick a button to see how Java searches for classes.
Java uses three main built-in class loaders:
// Demonstrating different Java Class Loaders
public class ClassLoaderDemo {
public static void main(String[] args) {
// Application Class Loader
ClassLoader appLoader = ClassLoaderDemo.class.getClassLoader();
System.out.println(appLoader);
// Extension Class Loader
System.out.println(appLoader.getParent());
// Bootstrap Class Loader (returns null)
System.out.println(appLoader.getParent().getParent());
}
}
The output shows:
null for bootstrap class loader (implemented in native code)