Class Loading in Java: Explanation & Process
Class loading is the process in which the JVM (Java Virtual Machine) loads Java classes into memory dynamically at runtime. This allows Java to be flexible and support features like reflection, dynamic class loading, and custom class loaders.
1. How Class Loading Works in JVM
When you execute a Java program (java MyClass
), the JVM loads the class into memory using a multi-step process:
Step-by-Step Class Loading Process
- Loading
- The Class Loader reads the
.class
file (bytecode) from disk, network, or another source. - It creates a Class object in the Method Area of JVM memory.
- The class is now available for execution.
- The Class Loader reads the
- Linking (Three Sub-Steps)
- Verification
- The Bytecode Verifier checks for illegal bytecode, memory corruption, or security violations.
- Ensures the class follows Java’s safety rules.
- Preparation
- Static variables are assigned default values (e.g.,
0
for integers,null
for objects).
- Static variables are assigned default values (e.g.,
- Resolution
- All symbolic references (e.g., references to other classes, methods, and fields) are resolved into actual memory addresses.
- Verification
- Initialization
- Static variables are initialized with actual values.
- Static blocks (
static {}
) are executed once when the class is loaded.
2. Types of Class Loaders in Java
Java provides different class loaders that load classes in a hierarchical manner.
A. Built-in Class Loaders
- Bootstrap ClassLoader
- Loads core Java classes from the
java.lang
package (e.g.,String
,Object
,Integer
). - Loads classes from rt.jar or the Java Standard Library.
- Loads core Java classes from the
- Extension ClassLoader
- Loads classes from the ext directory (
lib/ext
in Java 8). - Used for Java extensions (e.g., cryptography, security libraries).
- Loads classes from the ext directory (
- Application (System) ClassLoader
- Loads classes from the classpath (
CLASSPATH
environment variable or-cp
option). - Loads user-defined classes and libraries.
- Loads classes from the classpath (
B. Custom Class Loaders
- Java allows creating a Custom ClassLoader by extending
ClassLoader
class. - Useful for dynamic class loading, such as plugins or frameworks.
Example:
class MyClassLoader extends ClassLoader {
@Override
public Class<?> findClass(String name) {
// Custom logic to load the class
return super.findClass(name);
}
}
Example of Class Loading in Action
class Example {
static int x = 10; // need class, so it will be loaded
static {
System.out.println("Static block executed!");
x = 20; // need class, so it will be loaded
}
public static void main(String[] args) {
System.out.println("Value of x: " + x);
}
}
JVM Execution Flow
Example.class
is loaded into memory.- Static block executes (
x = 20
). main()
method runs, printing:
Static block executed!
Value of x: 20
4. How to Check Class Loading in Java
You can print the Class Loader for any class:
public class Test {
public static void main(String[] args) {
System.out.println(String.class.getClassLoader()); // null (Bootstrap Loader)
System.out.println(Test.class.getClassLoader()); // Application Loader
}
}
output
null
sun.misc.Launcher$AppClassLoader@18b4aac2
5. When Does Class Loading Happen?
A class is loaded only when needed, such as:
- When an object of the class is created.
- When a static variable is accessed.
- When a static method is called.
Summary
- Class loading dynamically loads Java classes into memory at runtime.
- Three main steps: Loading → Linking → Initialization.
- Three built-in ClassLoaders: Bootstrap, Extension, and Application.
- Java supports dynamic class loading for flexibility.