Java Reflection API in Java


The java.lang.Class class 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. Where it is used. The Reflection API is mainly used in:

IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc. Debugger Test Tools etc.

Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the JVM.

Java Reflection API is used for that purpose where it makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing their names at compile time.

And it also makes it possible to instantiate new objects, and to invoke methods using reflection.

Source Code

//Reflection in Java
 
import java.lang.reflect.*;
import java.util.Arrays;
 
class userDetails {
    private String name, city;
    private int age;
    public int rollno;
 
    public userDetails()
    {
        name="Ram Kumar";
        age=25;
        city="Salem";
    }
    public userDetails(String name,int age,String city)
    {
        this.name=name;
        this.age=age;
        this.city=city;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getCity() {
        return city;
    }
 
    public void setCity(String city) {
        this.city = city;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public void display()
    {
        System.out.println("Name : "+name);
        System.out.println("Age  : "+age);
        System.out.println("City : "+city);
        System.out.println("--------------------------");
    }
    private void Salary()
    {
        System.out.println("This is Private Salary Method");
    }
}
 
public class ReflectionDemo {
    public static void main(String[] args) {
 
        //Class o =student.class;
        System.out.println("----------------Class Details--------------");
        userDetails o =new userDetails("Raja",25,"Salem");
        Class c=o.getClass();
        System.out.println("Class Name              : "+c.getName());
        System.out.println("Check its is Interface  : "+c.isInterface());
        System.out.println("Check its is Array      : "+c.isArray());
 
        System.out.println("---------- Constructor Details-----------");
        Constructor[] constructors = c.getConstructors();
        for(Constructor co : constructors) {
            System.out.println("Constructor Name : " + co.getName());
            System.out.println("\tConstructor Parameters ");
            if(co.getParameterCount()==0){
                System.out.println("\t\tNo Arg Constructor");
            }else{
                Parameter [] parameters=co.getParameters();
                for(Parameter p : parameters) {
                    System.out.println("\t\t"+p.getName()+" "+p.getType());
                }
 
            }
        }
        System.out.println("--------------Method Details-------------");
        Method[] methods=c.getMethods();
        for(int i=0;i<methods.length;i++)
        {
            System.out.println("Method "+(i+1)+" : "+
                    Modifier.toString(methods[i].getModifiers()) +"  "+
                    methods[i].getReturnType().getName()+"  "+
                    methods[i].getName()+" - "+
                    Arrays.toString(methods[i].getParameters()));
        }
        System.out.println("--------------Declared Method Details-------------");
        Method[] decmethods=c.getDeclaredMethods();
        for(int i=0;i<decmethods.length;i++)
        {
            System.out.println("Method "+(i+1)+" : "+
                    Modifier.toString(decmethods[i].getModifiers()) +"  "+
                    decmethods[i].getReturnType().getName()+"  "+
                    decmethods[i].getName()+" - "+
                    Arrays.toString(decmethods[i].getParameters()));
        }
        System.out.println("--------------Fields Details-------------");
        //Field[] fields=c.getFields();  //Public Fields
        Field[] fields=c.getDeclaredFields();
        for(Field f : fields) {
            System.out.println(
                    Modifier.toString(f.getModifiers()) +"  "+
                    f.getType().getName()+"  "+
                    f.getName());
        }
    }
}
 

Output

javac ReflectionDemo.java
java ReflectionDemo.java

----------------Class Details--------------
Class Name              : userDetails
Check its is Interface  : false
Check its is Array      : false
---------- Constructor Details-----------
Constructor Name : userDetails
        Constructor Parameters
                No Arg Constructor
Constructor Name : userDetails
        Constructor Parameters
                arg0 class java.lang.String
                arg1 int
                arg2 class java.lang.String
--------------Method Details-------------
Method 1 : public  java.lang.String  getName - []
Method 2 : public  void  setName - [java.lang.String arg0]
Method 3 : public  void  display - []
Method 4 : public  void  setAge - [int arg0]
Method 5 : public  void  setCity - [java.lang.String arg0]
Method 6 : public  java.lang.String  getCity - []
Method 7 : public  int  getAge - []
Method 8 : public final  void  wait - [long arg0, int arg1]
Method 9 : public final  void  wait - []
Method 10 : public final native  void  wait - [long arg0]
Method 11 : public  boolean  equals - [java.lang.Object arg0]
Method 12 : public  java.lang.String  toString - []
Method 13 : public native  int  hashCode - []
Method 14 : public final native  java.lang.Class  getClass - []
Method 15 : public final native  void  notify - []
Method 16 : public final native  void  notifyAll - []
--------------Declared Method Details-------------
Method 1 : public  java.lang.String  getName - []
Method 2 : public  void  setName - [java.lang.String arg0]
Method 3 : public  void  display - []
Method 4 : public  void  setAge - [int arg0]
Method 5 : public  void  setCity - [java.lang.String arg0]
Method 6 : private  void  Salary - []
Method 7 : public  java.lang.String  getCity - []
To download raw file Click Here

Basic Programs