Skip to main content

10. Reflection in Java

📝 Reflection is a powerful feature in Java that allows you to inspect and manipulate classes, methods, fields, and other components at runtime, even if they are private or protected.

1. What is Reflection?

  • Reflection enables Java programs to analyze themselves and dynamically access classes and methods.
  • It is part of the java.lang.reflect package.

2. Benefits of Reflection

  • Dynamic Behavior: Allows the implementation of dynamic behavior, such as loading classes at runtime or invoking methods dynamically.
  • Access to Private Members: Can access private fields and methods, facilitating debugging and testing.
  • Framework Development: Useful in building frameworks like Hibernate and Spring that require dynamic type inspection.

3. Key Classes in Reflection

  • Class: Represents a class or interface in the Java runtime.
  • Field: Represents a field (member variable) of a class.
  • Method: Represents a method of a class.
  • Constructor: Represents a constructor of a class.

4. Basic Reflection Example

Here’s how to use reflection to inspect a class:

Example:

class Person {
private String name;
public int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}

// Reflection usage
public class ReflectionExample {
public static void main(String[] args) throws Exception {
// Get the Class object
Class<?> personClass = Class.forName("Person");

// Create an instance
Object personInstance = personClass.getConstructor(String.class, int.class).newInstance("Alice", 30);

// Access public method
Method displayMethod = personClass.getMethod("displayInfo");
displayMethod.invoke(personInstance);

// Access private field
Field nameField = personClass.getDeclaredField("name");
nameField.setAccessible(true); // Allow access to private field
System.out.println("Name (via reflection): " + nameField.get(personInstance));
}
}

5. Accessing Fields

You can access both public and private fields using reflection.

Example:

Field ageField = personClass.getField("age"); // Public field
int ageValue = ageField.getInt(personInstance);
System.out.println("Age: " + ageValue);

6. Accessing Methods

You can invoke methods using reflection, regardless of their access modifiers.

Example:

Method method = personClass.getDeclaredMethod("getName"); // Private method
method.setAccessible(true); // Allow access to private method
String name = (String) method.invoke(personInstance);
System.out.println("Name: " + name);

7. Performance Considerations

While reflection is powerful, it comes with performance overhead:

  • Reflection operations are slower than direct method calls.
  • Excessive use of reflection can lead to code that is hard to understand and maintain.

8. Summary of Reflection

FeatureDescription
Dynamic Class InspectionInspect classes, methods, and fields at runtime.
Access ModifiersCan access private and protected members.
Key ClassesClass, Field, Method, Constructor.
Performance OverheadSlower than direct access; use judiciously.
Use CasesFramework development, debugging, and dynamic behavior.

Reflection is a powerful tool in Java for accessing and manipulating classes and their members at runtime.