top
upGrad KnowledgeHut SkillFest Sale!

Search

Java Tutorial

REFLECTION IN JAVA 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. // Below program to show 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 above java program : 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) 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: Class.getDeclaredField(FieldName) : Used to get the private field. Returns an object type field for the specified field name.Field.setAccessible(true) : Allows access to the field to any control parameter used with the field. Fallback of reflection: Reflective code splits abstractions, so updates to the software can change behavior. 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: Debuggers scrutinize private class members using reflective property. An application can use external, user-defined classes by using their fully qualified names to create extensibility object instances. 
logo

Java Tutorial

Java Reflection

REFLECTION IN JAVA 

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

// Below program to show 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 above java program : 

  • 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) 
  • 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: 

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

Fallback of reflection: 

  • Reflective code splits abstractions, so updates to the software can change behavior. 
  • 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: 

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

protective orders in virginia

Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.

Johnfrance

Thank you for your valuable information.

sam

Thank you for this wonderful article!

Belay Abera

This article was really helpful to me, thank you!

dfsdf

super article!