The process of inspecting and changing the runtime behaviour of class during runtime is called as “Java Reflection”. The concept of Java reflection is often confused with the concept of introspection. Introspection can only examine the types and properties of an object during runtime. Java reflection can examine and modify the object during runtime.
The class java.lang.Class is utilized to procure the required metadata to modify and examine the runtime behaviour of the class. The packages like java.lang.reflect and java.lang provide classes required for Java reflection.
Java reflection enables the developer to inspect the classes without knowing the names of methods and classes. Java reflection is also used in instantiating objects and invoking new methods. To use the Java reflection, it is necessary to include the classes from java.lang.reflect package. While recapturing meta class data, java.lang.class is used instead of java.lang.reflect packages.
- SEE: Java interview Q&A available here
The following program is used an example for Java reflection:
Method[ ] methods= MyObj.class.getMethods();
for(Method method:methods)
{
System.out.println(“method = ” + method.getName());
}
In the above program, an object named class is acquired from a class named “MyObj”. Using the object “class”, the list of methods, iterates and names of methods present in that class is obtained.
The Java reflection is commonly used in
- Integrated Development Environment
- Debugger
- Test Tools
Popularly Used Methods of Class:
- public String getName() - returns the name of class
- public boolean isArray() - Check whether it is Array.
- public boolean isPrimitive() - Check whether it is Primitive
- public boolean isInterface() - Check whether it is interface
Aptitude questions and Answers here
Why Java Reflection is Required?
- To Examine the class of an object during runtime.
- To construct an object for a class during runtime.
- To arouse object’s method during runtime.
- To alter the accessibility flag of method, field and constructor.
Java reflection is a common method used in web framework. In web framework the developers will define an Interface implementations, classes and combine them together in configuration files. Dynamic initialization of classes is done using reflection.
How Java Reflection is Used?
The following program examples will give you an idea about using the Java reflection for various purposes.
Sample Code: 1
To obtain class name from an object.
package myreflection;
import java.lang.reflect.Method;
public class ReflectionHeloWrld
{
public static void main(String[] args)
{
Fu fi1 = new Fu();
System.out.println(fi1.getClass().getName());
}
}
class Fu
{
public void display()
{
System.out.println(“KVK”);
}
}
The output for the above program is myreflection.Fu.
Using the Java reflection the class name from an object is obtained.
Sample Code: 2
To initiate the method of an unknown object.
package myreflection;
import java.lang.reflect.Method;
public class ReflectionHeloWrld {
public static void main(String[] args)
{
Fu fi1 = new Fu();
Method method;
try {
method = fi1.getClass().getMethod(“print”, new Class<?>[0]);
method.invok1(fi1);
} catch (Exception e1)
{
e.printStakTrace();
}
}
}
class Fu {
public void display()
{
System.out.println(“KVK”);
}
}
The output for the above program is KVK
Image types of an object present in the example code is unknown. By using Java reflection, the code utilized the object to find the “print” method and called it.
- Download: Final year projects for free
Sample Code:3
To acquire a constructor and create an instance.
package myreflection;
import java.lang.reflect.Constructor;
public class ReflectionHeloWrld
{
public static void main(String[] args)
{
//create new instance of “Cls1”
Class<?> c1= null;
try
{
c=Cls1.forNam1(“myreflection.Fu”);
}catch(Exception e1)
{
e.printStakTrace();
}
//create new instance of “Fu”
Fu fi1 = null;
Fu fi2 = null;
//get all New constructors
Constructor<?>cons[] = c.getConstrctors();
try
{
fi1 = (Fu) cons[0].newInstan1();
fi2 = (Fu) cons[1].newInstan1(“kvk”);
} catch (Exception e1)
{
e.printStakTrace();
}
fi1.display();
fi2.display();
}
}
class Fu
{
String s1;
public Fu()
{
//Some text here
}
public Fu(String s1)
{
this.s1=s1;
}
public void print()
{
System.out.println(“s1”);
}
}
null, abc are the outputs of the above program.
In the above program, using Java reflection constructors are acquired and instances for the classes present in above program are also created.
The above set of codes are only examples for the application of Java reflection. The reflection have wide range applications in various programming languages like python, perl, Go and R.