Java - Java Reflection API – Detailed Explanation
Java Reflection API is a powerful feature in Java that allows a program to inspect and manipulate classes, interfaces, constructors, methods, and fields at runtime, even if their names are not known at compile time. In simple terms, it enables a Java program to analyze itself and modify its behavior dynamically while it is running.
This capability is part of the java.lang.reflect package and is commonly used in frameworks, libraries, and tools where flexibility and dynamic behavior are required, such as Spring, Hibernate, and testing frameworks like JUnit.
Core Concept of Reflection
Normally, Java is a statically typed language, meaning all classes, methods, and variables are known at compile time. Reflection breaks this limitation by allowing runtime inspection. For example, you can load a class dynamically, find out its methods, and even invoke them without knowing them in advance.
The entry point of reflection is the Class object. Every loaded class in Java has an associated Class object that holds metadata about that class.
Key Components of Reflection API
-
Class Class
TheClassclass is the backbone of reflection. It provides methods to get information about a class such as its name, methods, constructors, and fields.Example operations:
-
getName()returns the class name -
getMethods()returns all public methods -
getDeclaredFields()returns all fields declared in the class
-
-
Method Class
TheMethodclass represents a method of a class. Using it, you can:-
Inspect method name and parameters
-
Invoke methods dynamically using
invoke()
This allows calling methods at runtime without directly writing method calls in code.
-
-
Field Class
TheFieldclass represents member variables of a class. It allows:-
Reading field values
-
Modifying private or public fields dynamically
Even private fields can be accessed by disabling access checks using
setAccessible(true). -
-
Constructor Class
TheConstructorclass represents constructors of a class. It allows:-
Creating new objects dynamically at runtime
-
Choosing which constructor to use based on parameters
-
How Reflection Works (Step-by-Step)
-
Obtain the
Classobject of a class using:-
Class.forName("ClassName") -
or
Object.getClass()
-
-
Retrieve class information such as methods, fields, or constructors.
-
Use reflection methods to interact with those components.
-
Optionally modify access controls using
setAccessible(true). -
Invoke methods or create objects dynamically.
Example Use Case Conceptually
Suppose you have a class named Student with methods like getName() and setName(). With reflection, you can:
-
Load the
Studentclass at runtime -
Find all its methods
-
Create an object of
Student -
Call
setName()even if you did not directly code the method call
This is useful in situations where class details are not known beforehand.
Advantages of Reflection
-
Enables dynamic programming
-
Useful for frameworks and libraries
-
Helps in writing generic and reusable code
-
Allows runtime inspection and debugging tools
Disadvantages of Reflection
-
Slower performance compared to direct method calls
-
Breaks encapsulation by accessing private members
-
Harder to debug and maintain
-
Increases security risks if misused
Common Real-World Uses
-
Dependency Injection frameworks like Spring
-
Object-Relational Mapping tools like Hibernate
-
Testing frameworks like JUnit
-
Serialization libraries
-
Debugging and development tools
Summary
Java Reflection API is a mechanism that allows runtime inspection and manipulation of classes and objects. While it provides great flexibility and is essential for modern frameworks, it should be used carefully due to performance overhead and security considerations.