Lesson 31 • Advanced
Reflection API
Inspect and manipulate classes, methods, and fields at runtime — the engine behind Spring, Hibernate, and JUnit.
Before You Start
You should understand OOP & Interfaces (Lessons 9–11), Annotations basics, and JVM Internals (Lesson 30). Reflection operates at the JVM level, bypassing normal compile-time checks.
What You'll Learn
- ✅ Getting Class objects at runtime
- ✅ Inspecting fields, methods, and constructors
- ✅ Invoking methods dynamically
- ✅ Accessing private members with setAccessible
- ✅ Creating instances without the new keyword
- ✅ Performance implications and security concerns
1️⃣ What is Reflection?
💡 Analogy: Opening the Remote
Normally you use objects like a TV remote — press buttons without knowing internals. Reflection is like opening the case: you can see every chip and wire, and even rewire things. Powerful, but you can break things if you're not careful.
With reflection, code written before your class existed can still work with it. That's how Spring creates beans, Hibernate maps entities, and JUnit finds @Test methods — all without hardcoded references.
Use for: Frameworks, serialization, testing tools, plugin systems
Don't use for: Regular application logic — prefer interfaces and polymorphism
Try It: Inspecting & Invoking
// Reflection — Inspecting & Invoking
console.log("=== Reflection: Inspect & Invoke ===\n");
class User {
constructor(name, age) {
this.name = name;
this.age = age;
this._secret = "hidden123";
}
greet() { return "Hi, I'm " + this.name; }
getAge() { return this.age; }
_privateMethod() { return "secret: " + this._secret; }
}
let user = new User("Alice", 30);
// 1. Get class info
console.log("1. INSPECTING CLASS:");
console.log(" Class name:", user.co
...2️⃣ Core Reflection Operations
| Operation | API | Notes |
|---|---|---|
| Get class | Class.forName("pkg.MyClass") | By fully-qualified name |
| List methods | getDeclaredMethods() | Including private (not inherited) |
| Get field | field.get(obj) | May need setAccessible(true) |
| Invoke | method.invoke(obj, args) | Dynamic dispatch by string name |
| Create | constructor.newInstance() | No-arg or parameterized |
Try It: Dynamic Proxy & Framework Patterns
// Dynamic Proxy & Framework Patterns
console.log("=== Framework Patterns ===\n");
// 1. Simple DI container (like Spring)
console.log("1. MINI DEPENDENCY INJECTION CONTAINER:");
class DIContainer {
constructor() { this.registry = new Map(); }
register(name, factory) { this.registry.set(name, factory); }
resolve(name) {
let factory = this.registry.get(name);
if (!factory) throw new Error("No bean: " + name);
return factory(this);
}
}
class UserRepositor
...Try It: Performance & Caching
// Reflection Performance & Best Practices
console.log("=== Reflection Performance ===\n");
class Calculator {
add(a, b) { return a + b; }
multiply(a, b) { return a * b; }
}
let calc = new Calculator();
// 1. Direct vs reflective call performance
console.log("1. PERFORMANCE COMPARISON:");
let iterations = 100000;
let t1 = performance.now();
for (let i = 0; i < iterations; i++) calc.add(i, i);
let directTime = (performance.now() - t1).toFixed(2);
let methodName = "add";
let t2 = perf
...Common Mistakes
Method and Field objects if called repeatedly.Pro Tips
💡 MethodHandle (Java 7+) is faster than reflection and JIT-optimizable — use for performance-sensitive reflective calls.
💡 Proxy.newProxyInstance() creates interface implementations at runtime — the foundation of Spring AOP.
💡 Compile-time annotation processing (Lombok, MapStruct) generates code with zero runtime cost.
📋 Quick Reference
| Concept | API | Notes |
|---|---|---|
| Get Class | Class.forName("pkg.MyClass") | By fully-qualified name |
| List fields | getDeclaredFields() | Including private |
| Invoke | method.invoke(obj, args...) | Dynamic dispatch |
| Bypass access | setAccessible(true) | Use with caution |
| Proxy | Proxy.newProxyInstance() | Dynamic interface impl |
🎉 Lesson Complete!
You've mastered Java's Reflection API — the engine behind every major framework!
Next: Annotations — creating and processing metadata for cleaner, declarative code.
Sign up for free to track which lessons you've completed and get learning reminders.