Difference Between Static and Dynamic Class Loading in Java
In Java, class loading can be categorized into Static Class Loading and Dynamic Class Loading. The key difference lies in when and how the classes are loaded into memory.
1. Static Class Loading
What is it?
- Classes are loaded at compile time.
- Uses the
new
keyword to create instances. - The compiler checks dependencies at compile time, ensuring that all required classes are available.
- If a class is missing, the compiler throws an error.
Example of Static Class Loading
public class StaticLoading {
public static void main(String[] args) {
MyClass obj = new MyClass(); // Class is loaded at compile time
obj.showMessage();
}
}
class MyClass {
void showMessage() {
System.out.println("Hello from MyClass");
}
}
When to Use?
✅ When dependencies are known at compile time.
✅ When performance and efficiency are priorities (since preloading avoids runtime overhead).
2. Dynamic Class Loading
What is it?
- Classes are loaded at runtime (only when needed).
- Uses Reflection (
Class.forName()
) or customClassLoader
. - The compiler does not verify dependencies, so missing classes cause
ClassNotFoundException
at runtime. - Enables flexibility, such as loading plugins, JDBC drivers, or encrypted classes dynamically.
Example of Dynamic Class Loading
public class DynamicLoading {
public static void main(String[] args) {
try {
// Load class dynamically
Class<?> cls = Class.forName("MyClass");
// Create instance dynamically
Object obj = cls.getDeclaredConstructor().newInstance();
// Invoke method using Reflection
cls.getMethod("showMessage").invoke(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
}
📌 This does the same thing as the static example, but loads MyClass
at runtime instead of compile time.
Key Differences: Static vs. Dynamic Class Loading
Feature | Static Class Loading | Dynamic Class Loading |
---|---|---|
When Class is Loaded? | Compile time | Runtime |
How Class is Loaded? | Using new keyword | Using Class.forName() or ClassLoader |
Dependency Checking? | Done at compile time | Done at runtime |
Performance | Faster (preloaded) | Slightly slower (runtime resolution) |
Flexibility | Fixed dependencies | Can load unknown classes dynamically |
Example Use Cases | Regular object instantiation | JDBC, Plugin-based systems, Reflection |
When Should You Use Each?
✅ Use Static Class Loading When:
- All required classes are known at compile time.
- Performance is critical (avoids runtime overhead).
- You want strong type checking.
✅ Use Dynamic Class Loading When:
- You need flexibility (e.g., load plugins dynamically).
- Your app uses Reflection or JDBC.
- You are implementing hot-swapping, dependency injection, or encrypted class loading.
Real-World Examples
- JDBC Driver Loading (Dynamic)
// The exact database driver may change, so we load it dynamically.
Class.forName("com.mysql.cj.jdbc.Driver");
- Servlet Loading in Tomcat (Dynamic)
- Tomcat dynamically loads servlet classes instead of requiring them at compile time.
- Spring Framework (Dynamic)
- Spring dynamically loads beans using Reflection.
Final Thoughts
- Static class loading is the standard approach for most applications.
- Dynamic class loading is useful for flexible architectures like JDBC, plugins, and frameworks.
- If a dynamically loaded class is missing, it fails at runtime (
ClassNotFoundException
).
Would you like an example of a custom class loader for advanced dynamic loading?