10X Sale
kh logo
All Courses
  1. Tutorials
  2. Java

Java Reflection

Updated on Sep 2, 2025
 
32,980 Views

REFLECTION IN BAVA

Java Reflection is the method of evaluation and adjustment at runtime of all the capabilities of a class. Java's Reflection API is used to control class and its members at runtime, including fields, methods, constructor.

Reflection is an API for evaluating or altering the behavior of runtime methods, classes and interfaces. The Reflection package required, the "java.lang.reflect" package, allows us to invoke methods, regardless of the access modifier, at runtime.

Reflection in Java

// The below program shows the use of reflection in java

import java.lang.reflect.Method;  
import java.lang.reflect.Field;  
import java.lang.reflect.Constructor;  
class ReflectionTest 
{   
private String s;   
public ReflectionTest() { s = "Learning Reflection"; }  
public void myMethod1() {  
System.out.println("The string is " + s);  
}  
public void myMethod2(int n) {  
System.out.println("The number is " + n);  
}  
private void myMethod3() {  
System.out.println("Class private method invoked");  
}  
}  
class ReflectionDemo 
{  
public static void main(String args[]) throws Exception  
{   
ReflectionTestreflectionTest= new ReflectionTest();  
Class myCls = reflectionTest.getClass();  
System.out.println("The name of class is " +  
myCls.getName());  
Constructor constructor = myCls.getConstructor();  
System.out.println("The name of constructor is " +  
constructor.getName());  
System.out.println("The public methods of class are : ");  
Method[] methods = myCls.getMethods();  
for (Method method:methods)  
System.out.println(method.getName());  
Method methodcall1 = cls.getDeclaredMethod("myMethod2",  
int.class);  
methodcall1.invoke(obj, 2349);  
Field field = cls.getDeclaredField("s");  
field.setAccessible(true);  
field.set(obj, "My Country India");  
Method myMethodcall2 = cls.getDeclaredMethod("myMethod1");  
myMethodcall2.invoke(obj);  
Method myMethodcall3 = cls.getDeclaredMethod("myMethod3");  
methodcall3.setAccessible(true);  
myMethodcall3.invoke(obj);  
}  
}

Output:

The name of class is ReflectionTest 
The name of constructor is ReflectionTest 
The public methods of class are :  
myMethod2 
myMethod1 
The number is 2349 
The string is My Country India

Class private method invoked

Important observations based on the above Java program :

1. We can call / execute any technique / method by reflecting whether we recognize its procedure name and its parameter types. We are using two methods below for this purpose:

getDeclaredMethod() : Creating a method object to be invoked. For this method, the syntax is

Class.getDeclaredMethod(methodName, parameterType) 
methodName- the name of method whose object is to be created 
parameterType - parameter is an array of Class objects

invoke() : We use the method to invoke / execute a class method at runtime–

Method.invoke(Object, parameter) 

2. Through reflecting, with the aid of its class object, we can access a class's private variables and methods and invoke the function using the object as discussed above.

We are using 2 methods below for this purpose:

  1. Class.getDeclaredField(FieldName) : Used to get the private field. Returns an object type field for the specified field name.
  2. Field.setAccessible(true) : Allows access to the field to any control parameter used with the field.

Fallback of reflection:

  1. Reflective code splits abstractions, so updates to the software can change behavior.
  2. Reflective activity performance is slower than its non-reflective counterparts and should be avoided in performance-sensitive applications commonly referred to code segments.

Benefits of Using Reflection in Java:

  1. Debuggers scrutinize private class members using reflective property.
  2. An application can use external, user-defined classes by using their fully qualified names to create extensibility object instances.
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses