Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 3 OF 4 · 2 MIN READ

Class Loaders and Class Loading

Class loaders find and load classes into the JVM. Interviewers ask about the loader hierarchy, parent delegation, the loading, linking and initialisation phases, and the difference between ClassNotFoundException and NoClassDefFoundError.

What you will learn

  • Name the built-in class loaders
  • Explain parent delegation
  • Know the loading, linking and initialisation phases
  • Debug class-not-found errors

Key concepts

Built-in loaders

Bootstrap (core java.* classes, native code), Platform (other JDK modules) and Application/System (your classpath or module path).

Parent delegation

A loader first asks its parent; it loads the class itself only if the parent cannot. This keeps core classes like java.lang.String from being replaced.

Phases

Loading (read bytes), linking (verify, prepare static fields with defaults, resolve references) and initialisation (run static initialisers), which happens lazily on first active use.

Step by step

Two errors that look alike

  • ClassNotFoundException: a checked exception when code asks for a class by name (Class.forName, loadClass) and it is not found
  • NoClassDefFoundError: the class was present at compile time but missing at runtime, or its static initialiser failed earlier
  • Fix: check dependencies and scopes in Maven or Gradle (provided vs runtime), duplicate jars, and earlier ExceptionInInitializerError in the logs

Examples

Seeing class loaders

System.out.println(String.class.getClassLoader());          // null (bootstrap)
System.out.println(java.sql.Driver.class.getClassLoader()); // platform loader
System.out.println(MyService.class.getClassLoader());       // app loader

// java -verbose:class -jar app.jar   prints every class as it loads

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Explain when a static block runs: at load, at link or at first use.
  • Explain why Spring Boot fat jars need a custom class loader.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Can two classes with the same name exist in one JVM?

Yes, if different class loaders load them. A class is identified by its name plus its defining loader, which is how application servers isolate apps and how hot reload works. Casting between them fails with ClassCastException.

Summary

Bootstrap, platform and application loaders load classes with parent-first delegation, through loading, linking and lazy initialisation. Know ClassNotFoundException vs NoClassDefFoundError.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus