Skip to content
CampusEduX

JVM Internals & Memory · Lesson 124 of 150

Class Loading

Class loading is the process by which the JVM locates, reads, and loads a compiled .class file into memory, making the class available for use when a program needs it.

1 min read

01What is Class Loading?

Class loading is the process by which the JVM locates, reads, and loads a compiled .class file into memory, making the class available for use when a program needs it.

02Why is it used?

Java loads classes only when they are actually needed, rather than loading everything at once. This makes programs start faster and use memory more efficiently, only bringing in classes as the program actually requires them.

03Real-Life Example

Think of a library that fetches a specific book from storage only when a reader actually requests it, rather than placing every single book from the storage room out on display shelves from day one.

04Syntax

java
// Class loading happens automatically when a class is first referenced ClassName obj = new ClassName(); // triggers class loading if not already loaded

05Example Program

java
public class ClassLoadingDemo { public static void main(String[] args) { System.out.println("Main class loaded and running"); Helper h = new Helper(); // Helper class gets loaded here, when first used } } class Helper { Helper() { System.out.println("Helper class loaded and object created"); } }

Output:

Main class loaded and running
Helper class loaded and object created

06Key Points to Remember

  • Classes are loaded lazily — only when first actually referenced or used in the program.
  • Java uses a hierarchy of class loaders (Bootstrap, Platform, and Application class loaders) to load different categories of classes.
  • Class loading happens automatically; developers rarely need to trigger it manually in typical applications.