Java · OOP, JVM and backend craft

How does class loading work in the JVM?

HardclassloadingclassloaderjvmUpdated Oct 2026
Quiz this track

Short answer

The JVM loads, links and initializes classes on first active use. Class loaders delegate upward to parent loaders, which keeps core classes consistent and isolates application code.

Loading finds the bytecode and creates a Class object. Linking verifies bytecode safety, prepares static storage and resolves symbolic references. Initialization runs static initializers and static field assignments exactly once, on first active use.

The class loader hierarchy

  • Bootstrap loader loads core JDK classes and is represented as null
  • Platform loader loads modules such as java.sql and java.xml
  • Application loader loads the classpath or module path
  • Custom loaders support plugins, isolation and hot deployment
  • Parent delegation asks the parent first, then the child tries locally
java
Class<?> type = Class.forName("com.example.Plugin");
Object instance = type.getDeclaredConstructor().newInstance();

ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class<?> other = loader.loadClass("com.example.Plugin");
Class.forName initializes the class by default. loadClass loads it without running static initializers.

Parent delegation prevents application code from replacing core classes and keeps a class consistent within a loader. The same class name loaded by two different loaders produces two distinct Class objects, which causes ClassCastException even though the names match. That is common in plugin and application-server environments.

ClassNotFoundException means no loader could find the class by name. NoClassDefFoundError means the class was found earlier but its definition is unavailable now, often because a static initializer failed or a dependency is missing at runtime.

Static initializers run once per class loader, not once per JVM. A class reloaded by a new loader runs its static block again, which can duplicate registrations or open resources twice.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is parent delegation and why does it exist?
  2. 2What is the difference between loading, linking and initialization?
  3. 3Why can the same class name cause a ClassCastException?
  4. 4How do ClassNotFoundException and NoClassDefFoundError differ?

Say it in the interview

The JVM loads bytecode, links it through verification and preparation, then initializes static state on first active use. Class loaders follow parent delegation, so the bootstrap, platform and application loaders each ask their parent first. I use the Class forName method when I need initialization and plain loading when I do not. I also watch for duplicate classes across loaders, because they produce different Class objects.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Java mock interview and get scored feedback on depth, clarity and confidence.