Reflection is a powerful feature in Java that allows a program to examine and interact with classes, methods, constructors, fields, and annotations at runtime. It enables developers to inspect class metadata and invoke methods dynamically without knowing the class details during compilation.
Key Points: • Reflection allows runtime inspection of classes and objects. • It can access fields, methods, constructors, and annotations dynamically. • Reflection is heavily used by frameworks such as Spring, Hibernate, and JUnit. • It enables dynamic object creation and method invocation. • Excessive use of reflection can impact performance and reduce code readability.
Why Do We Need Reflection?
Normally, Java resolves classes and methods at compile time.
Example:
Employee employee = new Employee();
employee.display();Here, the class and method are known before execution.
With reflection:
• Classes can be loaded dynamically. • Methods can be invoked dynamically. • Object creation can happen at runtime.
This provides greater flexibility for framework development.
What Can Reflection Do?
Using reflection, we can:
• Retrieve class information • Access private fields • Invoke methods dynamically • Create objects at runtime • Read annotations • Inspect constructors
Example: Suppose an application receives a class name from a configuration file.
Configuration:
com.company.Employee
Using reflection:
• Load the class dynamically. • Create an object. • Invoke methods without hardcoding the class.
This makes the application more flexible.
Code Example:
import java.lang.reflect.Method;
public class Demo {
public static void main(String[] args)
throws Exception {
Class<?> clazz =
Class.forName("Employee");
Object object =
clazz.getDeclaredConstructor()
.newInstance();
Method method =
clazz.getMethod("display");
method.invoke(object);
}
}
class Employee {
public void display() {
System.out.println(
"Employee Method Invoked");
}
}Output:
Employee Method Invoked
Common Reflection Classes
1. Class
Represents metadata about a class.
Example:
Class<?> clazz =
Employee.class;2. Method
Represents a class method.
Example:
Method method =
clazz.getMethod("display");3. Field
Represents a class field.
Example:
Field field =
clazz.getDeclaredField("name");4. Constructor
Represents a class constructor.
Example:
Constructor<?> constructor = clazz.getConstructor();
Accessing Private Fields
Reflection can access private members by overriding access checks.
Example:
field.setAccessible(true);
Although powerful, this should be used carefully.
Real-World Usage of Reflection
Spring Framework
• Dependency Injection • Bean creation • Annotation processing
Hibernate
• Entity mapping • Dynamic object creation
JUnit
• Test discovery • Dynamic method execution
JSON Libraries
• Object serialization and deserialization
Advantages of Reflection
• Dynamic behavior at runtime • Flexible framework development • Reduced hardcoding • Easy annotation processing • Runtime object creation
Disadvantages of Reflection
• Slower than direct method calls • Breaks encapsulation when accessing private members • More difficult to debug • Reduced compile-time type checking
Reflection vs Normal Invocation
Normal Method Call
employee.display();
• Fast • Compile-time checked
Reflection
method.invoke(employee);• Dynamic • Runtime checked • Slightly slower
Real-World Example
In Spring Boot:
@Service
public class EmployeeService {
}Spring uses reflection to:
• Scan classes • Detect annotations • Create objects • Inject dependencies
Without reflection, many framework features would not be possible.
Interview Tip: A concise interview answer is:
"Reflection is a Java feature that allows a program to inspect and manipulate classes, methods, fields, constructors, and annotations at runtime. It enables dynamic object creation and method invocation and is widely used by frameworks such as Spring, Hibernate, and JUnit. Although powerful, reflection should be used carefully because it can affect performance and bypass normal access controls."