🌐 What is a Class Loader?
A class loader in Java is part of the JVM responsible for loading class files into memory when a program is running.
Java programs are compiled into .class files, and class loaders handle converting those files into in-memory Class objects that the JVM can work with.
✅ Why are Class Loaders Needed?
- Java allows dynamic loading of classes — meaning you don’t need to know all classes at compile time.
- Java supports loading classes from different sources — files, network, even databases.
- Class loaders make Java platform-independent — they abstract away file system specifics.
📊 The Class Loader Hierarchy
| Loader | Role | Loads From |
|---|---|---|
| Bootstrap ClassLoader | Core classes (java.lang.*, java.util.*, etc.) | JAVA_HOME/lib |
| Extension ClassLoader (deprecated after Java 9) | Loads classes from extension directories | JAVA_HOME/lib/ext |
| Application (System) ClassLoader | Loads application-level classes | Classpath (JARs, folders) |
| Custom ClassLoaders (optional) | Developer-defined class loaders | Custom locations (e.g., over network) |
📚 Example of the Hierarchy in Action
public class ClassLoaderExample {
public static void main(String[] args) {
// String is a core class, so it's loaded by the Bootstrap loader (null because it's native)
System.out.println(String.class.getClassLoader()); // null
// This application class is loaded by the Application ClassLoader
System.out.println(ClassLoaderExample.class.getClassLoader());
}
}
🚀 Key Roles of Class Loaders
| Role | Explanation |
|---|---|
| Loading Classes | Reads .class files and loads them into JVM memory |
| Delegation Model | Follows parent-first delegation (ask parent before trying itself) |
| Isolation | Keeps separate applications isolated by using different class loaders |
| Custom Loading | Can load classes from network, encrypted files, etc., via custom class loaders |
🔗 Parent Delegation Model (Important)
When a class loader is asked to load a class, it:
- Asks its parent first.
- Parent asks its own parent, all the way up to Bootstrap.
- If no parent can load it, the current class loader tries to load it itself.
This prevents custom or application class loaders from overriding core Java classes like java.lang.String.
⚙️ Example Flow
ClassLoaderExample.class.getClassLoader().loadClass("MyCustomClass");
Application class loader gets the request.
Application class loader asks parent (usually Bootstrap).
Bootstrap doesn’t know MyCustomClass, so Application loader handles it itself.
🔥 Custom Class Loader Example (Rare but Powerful)
class MyClassLoader extends ClassLoader {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (name.startsWith("com.myapp.secret")) {
// Load it from encrypted JAR or database (custom logic here)
}
return super.loadClass(name);
}
}
Use case: Dynamic plugins, hot reloading, or sandboxing.
📊 Summary Table
| Task | Handled By Class Loader? |
|---|---|
Finding .class files | ✅ Yes |
| Loading bytecode into memory | ✅ Yes |
| Linking (resolving references) | ✅ Involves class loader and JVM |
| Instantiating objects | ❌ No (that’s your code using new) |
⚠️ Important Notes for Interviews
✅ Every class in Java knows which class loader loaded it (accessible via getClassLoader()).
✅ Core classes (java.lang.*) are loaded by Bootstrap (which is native, so null is returned).
✅ You can create custom class loaders to load classes from encrypted files, remote servers, etc.
💡 Real-World Use Cases
| Case | Why Custom Class Loader? |
|---|---|
| App Servers (Tomcat, JBoss) | Each app gets its own class loader for isolation |
| Hot Deployment | Load new versions of classes without restarting JVM |
| Plugins | Dynamically load plugins at runtime |
| Security Sandboxing | Restrict which classes are even visible |
🎯 Final Pro Tip for Interviews
✅ Say: “Class loaders handle dynamic class loading in Java. They follow a parent-first delegation model to ensure system classes cannot be overridden by custom code.”
✅ Mention: “Bootstrap handles core classes, Application loader handles your app’s classes.”
✅ If asked for advanced details, mention custom class loaders for plugins, isolation, or encrypted loading.