«Back to Home

Core Java

Topics

Introduction Of Reflection API In Java

Reflection API
 
In Java, Reflection is a procedure of examining or modifying the run time behavior of a class at the run time. The java.lang.Class class gives many methods, which can be used to get metadata, examine and change the run time behavior of a class.
 
The java.lang package and java.lang.reflect package give classes for Java reflection.
 
Usage of Reflection API

The Reflection API is generally used in,
  • IDE (Integrated Development Environment) like Eclipse, MyEclipse, NetBeans

  • Debugger

  • Test Tools
java.lang.Class class

The java.lang.Class class is involved mainly in two tasks, which are,
  • This class helps the methods to get the metadata of a class at the run time.

  • This class helps the methods to examine and change the run time behavior of a class.
Methods of Class “class”

public String getName()

This method is used to return the class name.
 
public static Class forName(String className)throws ClassNotFoundException

This method is used to load the class and return the reference of Class “class.”
 
public Object newInstance()throws InstantiationException,IllegalAccessException

This method is used to create new instance.
 
public boolean isInterface()

This method is used to check, if it is an interface.
 
public boolean isArray()

This method is used to check, if it is an array.
 
public boolean isPrimitive()

This method is used to check, if it is primitive.
 
public Class getSuperclass()

This method is used to return the super class “class” reference.
 
public Field[] getDeclaredFields()throws SecurityException

This method is used to return the total number of fields of the class.
 
public Method[] getDeclaredMethods()throws SecurityException

This method is used to return the total number of the methods of the class.
 
public Constructor[] getDeclaredConstructors()throws SecurityException

This method is used to return the total number of the constructors of this class.
 
public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException

This method is used to return the method class object.
 
Get the object of Class class
 
There are three ways to get the instance of Class “class”, which are,
  • forName() method of Class class

  • getClass() method of Object class

  • the .class syntax
forName() method of Class class

This method is used to load the class dynamically and also return the object of Class “class”.
 
It should be used, if we know the full qualified name of the class and it cannot be used for primitive types.
 
Let's see an example of forName() method, given below,
 
Code
  1. class Reflection{}  
  2. public class Example1 {  
  3.     public static void main(String args[]) throws Exception {  
  4.         Class c = Class.forName("Reflection");  
  5.         System.out.println(c.getName());  
  6.     }  
  7. }  
69

getClass() method of Object class

This method is used to return the object of Class “class” and it should be used, if we know the type. Additionally, it can be used with primitives.
 
Let’s see an example of getClass() method, given below.
 
Code
  1. package reflectionexamples;  
  2. class Reflection {}  
  3. public class Example2 {  
  4.     void printName(Object obj) {  
  5.         Class c = obj.getClass();  
  6.         System.out.println(c.getName());  
  7.     }  
  8.     public static void main(String args[]) {  
  9.         Reflection r = new Reflection();  
  10.         Example2 e = new Example2();  
  11.         e.printName(r);  
  12.     }  
  13. }  
70

Output

71

The .class syntax

When a type is presented but there is no object, it is possible to obtain a Class by appending ".class" to the name of the type and it can be used for primitive data type also.
 
Let’s see an example of .class syntax.
 
Code
  1. package reflectionexamples;  
  2. public class Example3 {  
  3.     public static void main(String args[]) {  
  4.         Class c1 = boolean.class;  
  5.         System.out.println(c1.getName());  
  6.         Class c2 = Example3.class;  
  7.         System.out.println(c2.getName());  
  8.     }  
  9. }  
72

Output

73

Determine the class object

Some methods of Class “class” are used to determine the class object, which are,
 
public boolean isInterface()

This method is used to determine, if the particular class object represents an interface type.
 
public boolean isArray()

This method is used to determine, if the class object represents an array class.
 
public boolean isPrimitive()

This method is used to determine, if the particular class object represents a primitive type.
 
Let's see an example of reflection API to determine the object type, given below.
 
Code
  1. class Reflection {}  
  2. interface Example {}  
  3. public class ReflectionExample {  
  4.     public static void main(String args[]) {  
  5.         try {  
  6.             Class c1 = Class.forName("Reflection");  
  7.             System.out.println(c1.isInterface());  
  8.             Class c2 = Class.forName("Example");  
  9.             System.out.println(c2.isInterface());  
  10.         } catch (Exception e) {  
  11.             System.out.println(e);  
  12.         }  
  13.     }  
  14. }  
74

Summary

Thus, we learnt that Java Reflection is a procedure to examine or modify the run time behavior of a class at the run time and also learnt how we can use it in Java.