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 :
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)
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:
Benefits of Using Reflection in java:
Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.
Thank you for your valuable information.
Thank you for this wonderful article!
This article was really helpful to me, thank you!
super article!
Leave a Reply
Your email address will not be published. Required fields are marked *