Class Loading in Java: Explicit vs Implicit Class Loading with Examples

🔄 Class Loading in Java – Complete Beginner Guide
What is Class Loading in Java?

Class Loading is the process by which the Java Virtual Machine (JVM) loads a .class file from the hard disk into the JVM memory (Method Area / Class Area) during program execution.
Without class loading, Java classes cannot be executed.
Types of Class Loading in Java
Class loading in Java is broadly classified into two types:
Explicit Class Loading
Implicit Class Loading

1️⃣ Explicit Class Loading

What is Explicit Class Loading?
Explicit class loading occurs when the JVM loads a class automatically because the programmer directly accesses any class member.
When Does Explicit Class Loading Happen?
Accessing a static variable
Calling a static method
Creating an object using constructor
Example:
Student s = new Student(); // Class loaded explicitly
✔ The JVM loads the Student.class file when the object is created.

2️⃣ Implicit Class Loading

What is Implicit Class Loading?
Implicit class loading is a process where a class is loaded without directly accessing any class member.
The programmer explicitly instructs the JVM to load a class using a built-in method.
How to Achieve Implicit Class Loading?
Implicit class loading is achieved using the forName() method of the java.lang.Class class.

Syntax:

Class.forName("fully.qualified.ClassName");
Important Points About Class.forName()
It is a static method of java.lang.Class
Accepts a String parameter
The string must be the Fully Qualified Class Name
It loads the class without creating an object
It throws a checked exception

Exception in Implicit Class Loading
ClassNotFoundException

Class.forName() throws a ClassNotFoundException
It is a checked exception
Must be handled using try-catch or throws

Example:

try {
    Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

Real-Life Example of Implicit Class Loading
🎯 JDBC Driver Loading

Class.forName("com.mysql.cj.jdbc.Driver");
✔ The JDBC driver is loaded into memory
✔ No object is created
✔ Required for database connectivity

Difference Between Explicit and Implicit Class Loading

Feature.                      Explicit Loading.            Implicit Loading
Member Access.                    Yes.                            No
Object Creation.                      Yes.                           No
Method Used.   Constructor / Static access.    Class.forName()
Common Use.           Normal programs.            JDBC, frameworks

Interview & Exam Points

  • Class loading loads .class files into JVM
  • Explicit loading occurs automatically
  • Implicit loading uses Class.forName()
  • ClassNotFoundException is a checked exception
  • Fully qualified class name is mandatory

Comments

Popular posts from this blog

History of Java Programming Language | Features, Origin & Uses

Inheritance in Java: Types, Examples, and Explanation

Java Programming Features Every Beginner Should Know