{"id":3401,"date":"2020-05-28T18:59:14","date_gmt":"2020-05-28T13:29:14","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3401"},"modified":"2020-06-18T18:11:56","modified_gmt":"2020-06-18T12:41:56","slug":"what-is-java-reflections","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/what-is-java-reflections\/","title":{"rendered":"What is Java Reflections"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Java Reflection<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>It is a mechanism of examining or modifying the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/servlet-life-cycle\/\">run time behavior<\/a> of a class at run time.<\/li><li>&#8220;The java.lang.Class&#8221; file provides many methods that can be used to get metadata, examine and change the run time behavior of a class.<\/li><li>The java.lang and java.lang.reflect packages provide classes for java reflection.<\/li><\/ul>\n\n\n\n<p><strong>Program: 1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.lang.reflect.Method;&nbsp;\nimport java.lang.reflect.Field;&nbsp;\nimport java.lang.reflect.Constructor;&nbsp;\nclass TestReflection&nbsp;\n{&nbsp;\nprivate String str; \/\/ creating a private field&nbsp;\npublic Test ()&nbsp;&nbsp;\n{&nbsp;&nbsp;\nstr = \"Welcome to Reflection\";\n&nbsp;} \/\/ creating a public constructor&nbsp;\n&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;public void method_1() \/\/ Creating a public method with no arguments&nbsp;\n&nbsp;\n&nbsp;{&nbsp;&nbsp;&nbsp;\n&nbsp;System.out.println(\"The string is \" + str );&nbsp;\n&nbsp;&nbsp;}&nbsp;\n&nbsp;&nbsp;\n&nbsp;public void method_2(int number) \/\/ Creating a public method with int as argument&nbsp;\n&nbsp;\n&nbsp;{&nbsp;&nbsp;\nSystem.out.println(\"The number is \" + number);&nbsp;\n&nbsp;}&nbsp;\n&nbsp;&nbsp;\n&nbsp;private void method_3() \/\/ creating a private method\n{&nbsp;&nbsp;\n&nbsp;System.out.println(\"Private method called\");&nbsp;\n&nbsp;}&nbsp;\n&nbsp;}&nbsp;\n&nbsp;&nbsp;class DemoReflection&nbsp;\n{&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String arg[]) throws Exception&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TestReflection&nbsp; obj_t = new TestReflection ();&nbsp; \/\/ Creating object whose property is to be checked&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;Class cls_1 = obj_t.getClass();&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"The name of class is \" +&nbsp; cls_1.getName());\n&nbsp;&nbsp;&nbsp;&nbsp;Constructor constructor_1 = cls_1.getConstructor();&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"The name of constructor is \" + constructor_1.getName());&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"The public methods of the class are\");&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method [] methods_1 = cls_1.getMethods();&nbsp; &nbsp; \/\/ Getting methods of the class through the object&nbsp;\n&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (Method method1: methods_1)&nbsp; \/\/ Printing method names&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(method1.getName());&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method methodcall_1 = cls_1.getDeclaredMethod(\"method2\",&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int.class);&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;methodcall1.invoke(obj_t, 19);&nbsp; &nbsp; \/\/ invokes the method at runtime&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Field field = cls_1.getDeclaredField(\"str\");&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field.setAccessible(true); &nbsp; &nbsp; \/\/ allows the object to access the field irrespective&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field.set(obj_t, \"JAVA\");&nbsp; \/\/takes object and the new value to be assigned to the field as arguments&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method methodcall_2 = cls_1.getDeclaredMethod(\"method1\");&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;methodcall_2.invoke(obj_t);&nbsp; \/\/ invokes the method at runtime&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method methodcall_3 = cls_1.getDeclaredMethod(\"method3\");&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;methodcall_3. setAccessible(true);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ invokes the method at runtime&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;methodcall_3. invoke(obj_t);&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;\n}\n<\/pre>\n\n\n\n<p><strong><em>Output<\/em><\/strong>:<\/p>\n\n\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]<\/p>\n<p><span style=\"font-weight: 400;\">The name of the class is the Test<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The name of the constructor is Test<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The public methods of the class are:&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">method2<\/span><\/p>\n<p><span style=\"font-weight: 400;\">method1<\/span><\/p>\n<p><span style=\"font-weight: 400;\">wait<\/span><\/p>\n<p><span style=\"font-weight: 400;\">wait<\/span><\/p>\n<p><span style=\"font-weight: 400;\">wait<\/span><\/p>\n<p><span style=\"font-weight: 400;\">equals<\/span><\/p>\n<p><span style=\"font-weight: 400;\">toString<\/span><\/p>\n<p><span style=\"font-weight: 400;\">hashCode<\/span><\/p>\n<p><span style=\"font-weight: 400;\">getClass<\/span><\/p>\n<p><span style=\"font-weight: 400;\">notify<\/span><\/p>\n<p><span style=\"font-weight: 400;\">notifyAll<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The number is 19<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The string is JAVA<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Private method invoked<\/span><\/p>\n<p>[\/box]<\/p>\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Let&#8217;s see the example using the Get Interfaces<\/strong><\/h2>\n\n\n\n<p>We will use the getInterfaces() method of the class to collect information about the interfaces implemented by the class. This method returns an <a href=\"https:\/\/stackoverflow.com\/questions\/9256140\/array-of-interface-in-java\" rel=\"nofollow noopener\" target=\"_blank\">array of interfaces<\/a>.<\/p>\n\n\n\n<p><strong><em>Program<\/em><\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.lang.Class;\nimport java.lang.reflect.*;\ninterface Animal {\n&nbsp;&nbsp;&nbsp;public void display();\n}\ninterface Mammals {\n&nbsp;&nbsp;&nbsp;public void makeSound();\n}\n&nbsp;\nclass Dog implements Animal, Mammals {\n&nbsp;&nbsp;&nbsp;public void display() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Hello dog.\");\n&nbsp;&nbsp;&nbsp;}\n&nbsp;\n&nbsp;&nbsp;&nbsp;public void makeSound() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Bark bark\");\n&nbsp;&nbsp;&nbsp;}\n}\n&nbsp;\nclass ReflectionDemo {\n&nbsp;&nbsp;public static void main(String[] arg) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ create an object of Dog class\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog d1 = new Dog();\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ create an object of Class using getClass()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class obj = d1.getClass();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ find the interfaces implemented by Dog\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class[] objInterface = obj.getInterfaces();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(Class cl : objInterface) {\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ print the name of interfaces\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Interface Name is: \" + cl.getName());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception ex) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><strong><em>Output<\/em><\/strong><\/p>\n\n\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]<\/p>\n<p><span style=\"font-weight: 400;\">Interface Name is: Animal<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Interface Name is: Mammals<\/span><\/p>\n<p>[\/box]<\/p>\n\n\n<p><strong>3. Let&#8217;s learn the example using the Get Superclass and Access Modifier<\/strong><\/p>\n\n\n\n<p>We will use the method getSuperclass(), which can be used to retrieve the information about the superclass of a present class.<\/p>\n\n\n\n<p>The class also provides a method getModifier() that returns the modifier of class in integer form.<\/p>\n\n\n\n<p><strong>A<\/strong>) <strong>Program using Get Superclass and Access Modifier<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.lang.Class;\nimport java.lang.reflect.*;\n&nbsp;\ninterface Animal {\n&nbsp;&nbsp;&nbsp;public void display();\n}\n&nbsp;\npublic class Dog implements Animal {\n&nbsp;&nbsp;&nbsp;public void display() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Hello dog.\");\n&nbsp;&nbsp;&nbsp;}\n}\nclass Reflection_Demo {\n&nbsp;&nbsp;&nbsp;public static void main(String[] arg) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog dg = new Dog();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ create an object of Class using getClass()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class obj = dg.getClass();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int modifiers = obj.getModifiers();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Modifier: \" + Modifier.toString(modifier));\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Find the superclass of Dog\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class superClass = obj.getSuperclass();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Superclass: \" + superClass.getName());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><strong><em>Output<\/em><\/strong>:<\/p>\n\n\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]<\/p>\n<p><span style=\"font-weight: 400;\">Modifier: public<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Superclass: Animal<\/span><\/p>\n<p>[\/box]<\/p>\n\n\n<p><strong>B) Program using Accessing Public Field<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.lang.Class;\nimport java.lang.reflect.*;\nclass Dog {\n&nbsp;&nbsp;public String type;\n}\nclass ReflectionDemo {\n&nbsp;&nbsp;public static void main(String[] args) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog d1 = new Dog();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ create an object of the class Class\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class obj = d1.getClass();\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ manipulating the public field type of Dog&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Field field1 = obj.getField(\"type\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ set the value of field\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field1.set(d1, \"java\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String typeValue = (String)field1.get(d1);&nbsp; \/\/ get the value of field by converting in String\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"type: \" + typeValue);\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get the access modifier of type\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int mod1 = field1.getModifiers();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String modifier1 = Modifier.toString(mod1);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Modifier: \" + modifier1);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\" \");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><strong><em>Output<\/em><\/strong><\/p>\n\n\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]<\/p>\n<p><span style=\"font-weight: 400;\">type: labrador<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Modifier: public<\/span><\/p>\n<p>[\/box]<\/p>\n\n\n<h4 class=\"wp-block-heading\">Example: <strong>Accessing Private Field<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.lang.Class;\nimport java.lang.reflect.*;\n&nbsp;\nclass Dog {\n&nbsp;private String color;\n}\n&nbsp;\nclass ReflectionDemo {\npublic static void main(String[] args) {\n&nbsp;&nbsp;&nbsp;try {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog d1 = new Dog();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ create an object of the class Class\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class obj = d1.getClass();\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ accessing the private field\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Field field2 = obj.getDeclaredField(\"color\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ making the private field accessible\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field2.setAccessible(true);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ set the value of color\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field2.set(d1, \"brown\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String colorValue = (String)field2.get(d1); \/\/ get the value of type converting in String\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"color: \" + colorValue);\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get the access modifier of color\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int mod2 = field2.getModifiers();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String modifier2 = Modifier.toString(mod2);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"modifier: \" + modifier2);\n&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;catch(Exception e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();\n&nbsp;&nbsp;&nbsp;}\n&nbsp;}\n}<\/pre>\n\n\n\n<p><strong><em>Output<\/em><\/strong>:<\/p>\n\n\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]<\/p>\n<p><span style=\"font-weight: 400;\">color: brown<\/span><\/p>\n<p><span style=\"font-weight: 400;\">modifier: private<\/span><\/p>\n<p>[\/box]<\/p>\n\n\n<h4 class=\"wp-block-heading\">Example: Method Reflection<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.lang.Class;\nimport java.lang.reflect.*;\n&nbsp;\nclass Dog {\n&nbsp;&nbsp;&nbsp;public void display() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"I am a dog.\");\n&nbsp;&nbsp;&nbsp;}\n&nbsp;\n&nbsp;&nbsp;&nbsp;protected void eat() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"I eat dog food.\");\n&nbsp;&nbsp;&nbsp;}\n&nbsp;\n&nbsp;&nbsp;&nbsp;private void makeSound() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Bark Bark\");\n&nbsp;&nbsp;&nbsp;}\n&nbsp;\n}\n&nbsp;\nclass ReflectionDemo {\n&nbsp;&nbsp;&nbsp;public static void main(String[] args) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog d1 = new Dog(); &nbsp; \/\/ create an object for the&nbsp; Dog class\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ create an object of class\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class obj = d1.getClass();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get all the methods using the getDeclaredMethod()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method[] methods = obj.getDeclaredMethods();\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get the name of methods\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(Method m : methods) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Method Name is: \" + m.getName());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get the access modifier of methods\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int modifier = m.getModifiers();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Modifier is: \" + Modifier.toString(modifier));\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get the return types of method\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"The return type is void\" + m.getReturnType());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\" \");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception exc) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exc.printStackTrace();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><strong><em>Output<\/em><\/strong>:<\/p>\n\n\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]<\/p>\n<p><span style=\"font-weight: 400;\">Method Name is: display<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Modifier is: public<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The return type is void.<\/span><\/p>\n<p><\/p>\n<p><span style=\"font-weight: 400;\">Method Name is: eat<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The modifier is: protected<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The return type is void.<\/span><\/p>\n<p><\/p>\n<p><span style=\"font-weight: 400;\">Method Name is: makeSound<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The modifier is: private<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The return type is void.<\/span><\/p>\n<p>[\/box]<\/p>\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Let&#8217;s have an example using Reflection of Constructor<\/strong><\/h2>\n\n\n\n<p>We will use the different- different constructors of a class by using the various methods which are provided by the Constructor class.<\/p>\n\n\n\n<p>a) getConstructors() &#8211; returns all <a href=\"https:\/\/www.w3schools.com\/java\/java_constructors.asp\" rel=\"nofollow noopener\" target=\"_blank\">public constructors of a class <\/a>and superclass of the class<\/p>\n\n\n\n<p>b) getDeclaredConstructor() &#8211; returns all the constructors<\/p>\n\n\n\n<p>c) getName() &#8211; returns the name of constructors<\/p>\n\n\n\n<p>d) getModifiers() &#8211; returns the access modifier of constructors in integer form<\/p>\n\n\n\n<p>e) getParameterCount() &#8211; returns the number of parameters of constructors<\/p>\n\n\n\n<p><strong><em>Program using Constructor reflection<\/em><\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.lang.Class;\nimport java.lang.reflect.*;\nclass Dog {\n&nbsp;&nbsp;&nbsp;public Dog() {\n&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;public Dog(int age) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;private Dog(String sound, String type) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;}\n}\n&nbsp;\nclass ReflectionDemo {\n&nbsp;&nbsp;&nbsp;public static void main(String[] args) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog d1 = new Dog();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class obj = d1.getClass();\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get all the constructors in a class using getDeclaredConstructor()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructor[] constructors = obj.getDeclaredConstructors();\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(Constructor c : constructors) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get names of constructors\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Constructor Name: \" + c.getName());\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get access modifier of constructors\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int modifier = c.getModifiers();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Modifier: \" + Modifier.toString(modifier));\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get the number of parameters in constructors\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Parameters: \" + c.getParameterCount());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><strong><em>Output<\/em><\/strong>:<\/p>\n\n\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]<\/p>\n<p><span style=\"font-weight: 400;\">Constructor Name: Dog<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Modifier: public<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Parameters: 0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Constructor Name: Dog<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Modifier: public<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Parameters: 1<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Constructor Name: Dog<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Modifier: private<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Parameters: 2<\/span><\/p>\n<p>[\/box]<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java Reflection It is a mechanism of examining or modifying the run time behavior of a class at run time. &#8220;The java.lang.Class&#8221; file provides many methods that can be used to get metadata, examine and change the run time behavior of a class. The java.lang and java.lang.reflect packages provide classes for java reflection. Program: 1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3784,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[840,842,841],"class_list":["post-3401","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-java-reflection","tag-method-reflection","tag-using-get-interfaces"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3401"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3401\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3784"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}