Using reflection to inspect and modify classes at runtime
Reflection is one of the most advanced Java concepts or features that allows Java developers to inspect and modify the class, method, field, and constructor objects of a user-defined program during execution or runtime.

Reflection is primarily a built-in feature of the java.lang.reflect package library in Java.
Reflection is a framework used in Java programs for dependency injection, testing tools, serialization libraries, and ORM frameworks.
Getting Class Information in Java Programs.
There are three common methods for getting a user-defined class object in Java.
Using .class in Java.
Class<?> cls = Employee.class;
Using the getClass() method.
Employee employee = new Employee();
Class<?> cls = employee.getClass();
Using Class.forName().
Class<?> cls =
Class.forName(“com.example.Employee”);
The Class.forName() function method is useful when the class name is known as a string at the Java runtime.
Viewing class information in a Java program.
Suppose we have a class named Employee.
import java.lang.reflect.Method;
class Employee {
private int emp_id;
private String emp_name;
public Employee() {
}
public void preview() {
System.out.println(“Employee”);
}
public void dept(String department) {
System.out.println(“Employee ” + department);
}
Here, we can inspect this concept using reflection.
public static void main(String[] args) {
Class<?> cls = Employee.class;
// Inspect class name using Reflection
System.out.println(cls.getSimpleName());
// Create object and call preview()
Employee emp = new Employee();
emp.preview();
}
}
It’s output.
Employee
Employee
Inspecting Java Program Fields.
The getDeclaredFields() function method in the current Java program returns the values of the fields declared in the class.
Class<?> cls = Employee.class;
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
System.out.println(field.getemp_name());
}
It’s output.
id
emp_name
Java users can also access information about class fields.
for (Field field : fields) {
System.out.println(
field.gettemp_name() + ” : ” +
field.getType()
);
}
Reading a private field in a Java program.
Reflection allows Java users to access private data member information, subject to the class’s access-control/module restrictions.
Employee employee = new Employee();
Field field =
Employee.class.getDeclaredField(“emp_name”);
field.setAccessible(true);
String emp_name =
(String) field.get(employee);
System.out.println(emp_name);
Here, in the above program, the setAccessible(true) function requests reflective access to a normally inaccessible member. In a strongly encapsulated module, this can be restricted as needed.
Modifying a Field in a Java Program.
Reflection in a Java program can also modify a field of a class.
Employee employee = new Employee();
Field field =
Employee.class.getDeclaredField(“emp_name”);
field.setAccessible(true);
field.set(Employee, “Siddhi”);
Now, emp_name has a new value.
Siddhi
For example.
System.out.println(
field.get(employee)
);
Its output.
Siddhi
Verifying a method in Java.
In a Java program, the getDeclaredMethods() function returns a method declared by the Method class.
Class<?> cls = Employee.class;
Method[] methods =
cls.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.getemp_name());
}
It’s output.
preview
department
Java users can also see the return type.
for (Method method : methods) {
System.out.println(
method.getemp_name() +
” -> ” +
method.getReturnType()
);
}
Invoking a method using reflection in Java.
In a Java program, a method can be called dynamically using the invoke() function.
Employee employee = new Employee();
Method method =
Employee.class.getDeclaredMethod(“preview”);
method.invoke(employee);
Its output.
employee
This is an important method in this.
method.invoke(object);
Invoking a method with parameters in Java.
Here let us assume that we have a class condition.
public void dept(String department) {
System.out.println(“employee ” + department);
}
We can invoke it using.
Employee employee = new Employee();
Method method =
Employee.class.getDeclaredMethod(
“employee”,
String.class
);
method.invoke(employee, “sales”);
Its output.
sales department
Here in this example.
String.class
denotes the parameter type.
Inspecting Constructors in Java.
Java users can inspect constructors in their programs by using the getDeclaredConstructors() function method.
Class<?> cls = Employee.class;
Constructor<?>[] constructors =
cls.getDeclaredConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
Creating objects using reflection in Java.
The constructor method can be used to create objects in a Java program.
Constructor<Employee> constructor =
Employee.class.getDeclaredConstructor();
Employee employee =
constructor.newInstance();
This dynamically creates a new Employee object in the current Java instance.
Complete example of using reflection to inspect and modify classes at runtime.
import java.lang.reflect.*;
class Employee {
private String emp_name = “Random”;
public void preview() {
System.out.println(“Employee Name -: ” + emp_name);
}
}
public class Main {
public static void main(String[] args)
throws Exception {
//here we Get employee Class object
Class<?> cls = Employee.class;
// here we Create a class object
Employee employee =
(Employee) cls.getDeclaredConstructor()
.newInstance();
//here we Access class field
Field field =
cls.getDeclaredField(“emp_name”);
field.setAccessible(true);
// Here we modify the employee field
field.set(employee, “Bhavishi Deora”);
// Here we invoke the method
Method method =
cls.getDeclaredMethod(“preview”);
method.invoke(employee);
}
}
It’s output.
Employee Name -: Bhavishi Deora
What’s in this?
Employee.class
↓
Get Employee class information
↓
Create a new Class object
↓
Find the private field “emp_name”
↓
Modify the Class field
↓
Find the preview() method
↓
Invoke the Class method
Most popular important reflection java supported classes
| # | Most reflection classes | Purpose of each reflection class |
| 1. | Class | It Represents a user define class at java program execution or runtime |
| 2. | Field | It Represents a user define custom class with its desire class data type field |
| 3. | Method | It Represents a user define class method to call in reflection when user need |
| 4. | Constructor | It Represents a user define custom class constructor data object method when user need |
| 5. | Modifier | It Provides detail information about user define class constructor modifiers task |
Example of a Class field modification.
Modifier.isPrivate(field.getModifiers());
It checks whether a field is private or not in the current program.
GetMethods() vs. getDeclaredMethods() explanation in Java.
Both of these method functions are important in the Java reflection concept.
getMethods() Java reflection concept.
This is a public function method in a Java program, including inherited public methods.
Method[] method =
Employee.class.getMethods();
getDeclaredMethods() Java reflection concept.
This function method provides methods declared directly in the class, including non-public methods.
Method[] method =
Employee.class.getDeclaredMethods();
Similarly, the.
getFields() function.
→ It contains public class fields.
getDeclaredFields() function.
→ Contains the declared fields in a class.
getMethods() function.
→ This is a class public method, which also includes inherited methods.
getDeclaredMethods() function.
→ Contains the methods declared in the class.
Advantages of reflection in Java.
- Java reflection programs allow inspection of a class at runtime.
- Java reflection can dynamically invoke class methods.
- Reflection methods can reflectively access class fields and constructors.
- Reflection features are useful for class frameworks and libraries.
- Reflection features support dynamic and flexible applications in Java.
Disadvantages of reflection in Java.
- Reflection makes normal Java program source code more complex.
- Using reflection can slow down the processing of direct Java class method calls.
- Reflection can bypass normal encapsulation tasks by allowing access to class data.
- Errors normally caught at compile time by reflection may be exposed at runtime.
- Reflection Concept Strong Java module encapsulation can block reflective access.
Conclusion of Using Reflection to Inspect and Modify Classes at Runtime.
Reflection helps Java programs dynamically interact with user-defined class data type elements at runtime.
Detailed Class Reflection Concept.
↓
- Class Fields → Inspect/Read/Modify
- Class Methods → Inspect/Invoke
- Class Constructors → Inspect/Create Objects
These are the main reflection APIs to remember in Java.
- User-defined class prototype
- Class field member data type
- Class method type
- Class constructor object
Some of the most commonly used basic operations in Java reflection are.
- getDeclaredFields() function method.
- getDeclaredMethods() function method.
- getDeclaredConstructors() function method.
- getDeclaredField() function method.
- getDeclaredMethod() function method.
- getDeclaredConstructor() function method.
- field.get() function method.
- field.set() function method.
- method.invoke() function method.
- constructor.newInstance() function method.

