Click here to watch in Youtube :
https://www.youtube.com/watch?v=dmDPT7Y22OM&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
Java Reflection in details and how to get the metadata information of the class | Reflection in java |
Java Reflection in details and how to get the metadata information of the class | Reflection in java |
Java Reflection in details and how to get the metadata information of the class | Reflection in java |
class Display { private String str; public Display() { str = "Welcome"; } public void method1() { System.out.println("Inside method1,The string is " + str); } public void method2(int n) { System.out.println("\nInside method2,The number is " + n); } private void method3() { System.out.println("Private method invoked"); } }ReflectionDemo.java
import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; /* * A simple Java program to demonstrate the use of reflection. */ public class ReflectionDemo { public static void main(String[] args) throws Exception { /* * Creating object whose property is to be checked */ Display displayObj = new Display(); /* * Creating class object from the object using getclass method */ Class<? extends Display> classObj = displayObj.getClass(); System.out.println("The name of class is " + classObj.getName()); /* * Getting the constructor of the class through the object of the class */ Constructor<? extends Display> constructor = classObj.getConstructor(); System.out.println("The name of constructor is " + constructor.getName()); System.out.println("\nThe public methods of class are : "); /* * Getting methods of the class through the object of the class by using * getMethods */ Method[] methodArray = classObj.getMethods(); /* * Printing method names */ for (Method method : methodArray) { System.out.println(method.getName()); } /* * Creates object of desired method by providing the method name and * parameter class as arguments to the getDeclaredMethod */ Method method2Obj = classObj.getDeclaredMethod("method2", int.class); /* * invokes the method at runtime */ method2Obj.invoke(displayObj, 19); /* * Creates object of the desired field by providing the name of field as * argument to the getDeclaredField method */ Field field = classObj.getDeclaredField("str"); /* * Allows the object to access the field irrespective of the access * specifier used with the field */ field.setAccessible(true); /* * Takes object and the new value to be assigned to the field as * arguments */ field.set(displayObj, "JAVA"); /* * Creates object of desired method by providing the method name as * argument to the getDeclaredMethod */ Method method1Obj = classObj.getDeclaredMethod("method1"); /* * invokes the method at runtime */ method1Obj.invoke(displayObj); /* * Creates object of the desired method by providing the name of method * as argument to the getDeclaredMethod method */ Method method3Obj = classObj.getDeclaredMethod("method3"); /* * Allows the object to access the method irrespective * of the access specifier used with the method */ method3Obj.setAccessible(true); /* * invokes the method at runtime */ method3Obj.invoke(displayObj); } }Output
The name of class is Display The name of constructor is Display The public methods of class are : method2 method1 wait wait wait equals toString hashCode getClass notify notifyAll Inside method2,The number is 19 Inside method1,The string is JAVA Private method invokedClick the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_Intro_2_details.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_Intro_2_details
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/7b0b4fbacd011a10dc23b42c4acc3ae6988db782/BasicJava/ReflectionDemo_Intro_2_details/?at=master
See also:
No comments:
Post a Comment