Java Reflection is an API. This API is used to examine or modify the run time behavior of a class, method or interface. This scenario is called Java Reflection. The packages that provide tools for Java Reflection are java.lang and java.lang.reflect packages. These packages further contain the classes like java.lang.Class. This class contains methods that fetch the metadata and can be used to examine the behavior of a class on run time.
Java Reflection provides the purpose of debugging in debuggers. It is used in IDEs (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc. It can also be used in testing tools.
With reflection, a programmer can access private variables and methods of a class. The below code shows the concept of Java Reflection.
// A simple Java code to demonstrate the concept of reflection import java.lang.reflect.Method; import java.lang.reflect.Field; import java.lang.reflect.Constructor; // Defining class to create objects class Example { // create a private field private String s; // create a public constructor public Example() { s = "Tutorialsart Java"; } // Create a public method with no arguments public void method1() { System.out.println("The string is " + s); } // Create a public method with int type argument public void method2(int n) { System.out.println("The number is " + n); } // create a private method private void method3() { System.out.println("Private method is invoked here"); } } class Demo { public static void main(String args[]) throws Exception { // Create object whose property is to be checked Example obj = new Example(); // Creating class object from the object using // getclass method Class cls = obj.getClass(); System.out.println("The name of class is " + cls.getName()); // Get the constructor of the class through the // object of the class Constructor constructor = cls.getConstructor(); System.out.println("The name of constructor is " + constructor.getName()); System.out.println("The public methods of class are : "); // Get methods of the class through the object // of the class by using getMethods Method[] methods = cls.getMethods(); // Print method names for (Method method:methods) System.out.println(method.getName()); // create object of desired method by providing the // method name and parameter class as arguments to // the getDeclaredMethod Method methodcall1 = cls.getDeclaredMethod("method2", int.class); // invoke the method at runtime methodcall1.invoke(obj, 55); // create object of the desired field by providing // the name of field as argument to the // getDeclaredField method Field field = cls.getDeclaredField("s"); // allow the object to access the field irrespective // of the access specifier used with the field field.setAccessible(true); // take object and the new value to be assigned // to the field as arguments field.set(obj, "JAVA"); // Create object of desired method by providing the // method name as argument to the getDeclaredMethod Method methodcall2 = cls.getDeclaredMethod("method1"); // invoke the method at runtime methodcall2.invoke(obj); // Create object of the desired method by providing // the name of method as argument to the // getDeclaredMethod method Method methodcall3 = cls.getDeclaredMethod("method3"); // allows the object to access the method irrespective // of the access specifier used with the method methodcall3.setAccessible(true); // invokes the method at runtime methodcall3.invoke(obj); } }
The getClass() method gets the name of the class to which the object belongs. The getConstructor() method gets the public constructors of the object of the class. The getMethods() method gets the public methods of the class, object relates to. The output of the above code after compilation and execution is: