☕ Java Advanced

What is the ClassLoader in Java?

Why Interviewers Ask This

This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.

Answer

A ClassLoader is responsible for loading Java class files (.class bytecode) into the JVM at runtime. The JVM uses a delegation hierarchy: Bootstrap ClassLoader (loads core Java classes from rt.jar/modules), Extension/Platform ClassLoader (loads extension libraries), and Application ClassLoader (loads classes from the classpath). When a class is requested, the child loader asks the parent first (delegation model) — this prevents malicious classes from replacing core Java classes. You can create custom ClassLoaders for dynamic class loading, hot deployment (reloading classes without restarting), or loading from non-standard sources (databases, networks). This is how application servers like Tomcat isolate different web applications.

Pro Tip

Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Java answers easy to follow.