|
| 1 | +// Java Program to demonstrate the Use of Reflection |
| 2 | + |
| 3 | +// Importing required classes |
| 4 | +import java.lang.reflect.Constructor; |
| 5 | +import java.lang.reflect.Field; |
| 6 | +import java.lang.reflect.Method; |
| 7 | + |
| 8 | +// Class 1 |
| 9 | +// Of Whose object is to be created |
| 10 | +class Test { |
| 11 | + // creating a private field |
| 12 | + private String s; |
| 13 | + |
| 14 | + // Constructor of this class |
| 15 | + |
| 16 | + // Constructor 1 |
| 17 | + // Public constructor |
| 18 | + public Test() { s = "GeeksforGeeks"; } |
| 19 | + |
| 20 | + // Constructor 2 |
| 21 | + // no arguments |
| 22 | + public void method1() |
| 23 | + { |
| 24 | + System.out.println("The string is " + s); |
| 25 | + } |
| 26 | + |
| 27 | + // Constructor 3 |
| 28 | + // int as argument |
| 29 | + public void method2(int n) |
| 30 | + { |
| 31 | + System.out.println("The number is " + n); |
| 32 | + } |
| 33 | + |
| 34 | + // Constructor 4 |
| 35 | + // Private method |
| 36 | + private void method3() |
| 37 | + { |
| 38 | + System.out.println("Private method invoked"); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Class 2 |
| 43 | +// Main class |
| 44 | +class ReflectionDemo { |
| 45 | + |
| 46 | + // Main driver method |
| 47 | + public static void main(String args[]) throws Exception |
| 48 | + { |
| 49 | + // Creating object whose property is to be checked |
| 50 | + |
| 51 | + // Creating an object of class 1 inside main() |
| 52 | + // method |
| 53 | + Test obj = new Test(); |
| 54 | + |
| 55 | + // Creating class object from the object using |
| 56 | + // getClass() method |
| 57 | + Class cls = obj.getClass(); |
| 58 | + |
| 59 | + // Printing the name of class |
| 60 | + // using getName() method |
| 61 | + System.out.println("The name of class is " |
| 62 | + + cls.getName()); |
| 63 | + |
| 64 | + // Getting the constructor of the class through the |
| 65 | + // object of the class |
| 66 | + Constructor constructor = cls.getConstructor(); |
| 67 | + |
| 68 | + // Printing the name of constructor |
| 69 | + // using getName() method |
| 70 | + System.out.println("The name of constructor is " |
| 71 | + + constructor.getName()); |
| 72 | + |
| 73 | + // Display message only |
| 74 | + System.out.println( |
| 75 | + "The public methods of class are : "); |
| 76 | + |
| 77 | + // Getting methods of the class through the object |
| 78 | + // of the class by using getMethods |
| 79 | + Method[] methods = cls.getMethods(); |
| 80 | + |
| 81 | + // Printing method names |
| 82 | + for (Method method : methods) |
| 83 | + System.out.println(method.getName()); |
| 84 | + |
| 85 | + // Creates object of desired method by |
| 86 | + // providing the method name and parameter class as |
| 87 | + // arguments to the getDeclaredMethod() method |
| 88 | + Method methodcall1 |
| 89 | + = cls.getDeclaredMethod("method2", int.class); |
| 90 | + |
| 91 | + // Invoking the method at runtime |
| 92 | + methodcall1.invoke(obj, 19); |
| 93 | + |
| 94 | + // Creates object of the desired field by |
| 95 | + // providing the name of field as argument to the |
| 96 | + // getDeclaredField() method |
| 97 | + Field field = cls.getDeclaredField("s"); |
| 98 | + |
| 99 | + // Allows the object to access the field |
| 100 | + // irrespective of the access specifier used with |
| 101 | + // the field |
| 102 | + field.setAccessible(true); |
| 103 | + |
| 104 | + // Takes object and the new value to be assigned |
| 105 | + // to the field as arguments |
| 106 | + field.set(obj, "JAVA"); |
| 107 | + |
| 108 | + // Creates object of desired method by providing the |
| 109 | + // method name as argument to the |
| 110 | + // getDeclaredMethod() |
| 111 | + Method methodcall2 |
| 112 | + = cls.getDeclaredMethod("method1"); |
| 113 | + |
| 114 | + // Invokes the method at runtime |
| 115 | + methodcall2.invoke(obj); |
| 116 | + |
| 117 | + // Creates object of the desired method by providing |
| 118 | + // the name of method as argument to the |
| 119 | + // getDeclaredMethod() method |
| 120 | + Method methodcall3 |
| 121 | + = cls.getDeclaredMethod("method3"); |
| 122 | + |
| 123 | + // Allows the object to access the method |
| 124 | + // irrespective of the access specifier used with |
| 125 | + // the method |
| 126 | + methodcall3.setAccessible(true); |
| 127 | + |
| 128 | + // Invoking the method at runtime |
| 129 | + methodcall3.invoke(obj); |
| 130 | + } |
| 131 | +} |
0 commit comments