Blog Image

Reflection API  

This blog i am explaining about reflection API and what are the drawbacks and using this API how we can get the meta data of  class and which classes are providing for getting the meta data of class and methods

Reflection API:

Reflection API is a process of examining or modifying the behavior of a class at run time.

Reflection is powerful but we should not use in normal programming because of fallowing reasons,

  • Performance impact because it scanning the all the class path to find the classes to load ,   performance will degrated.
  • Security threat also there because of at run time it will accessible, means if any code changes entire application will impacted.
Reflection in java little use in normal programming but is the backbone of most of the java and j2ee frameworks .
If you want to know the information related to the classes java.lang.reflect package provides many classes to implement reflection java.Methods of the java.lang.Class class is used to gather the complete metadata of a particular class.
To know the information about the variables ,method and constructor first we want object of a class,
so we can create a object of class by using,
  • new keyword ,  
  • class.forName()
  • .class.
see below example for creating the object,

  Class c1=Class.forName("com.sjg.reflection.ReflectionApiExample");
        ReflectionApiExample reflectionApi = new ReflectionApiExample();
        Class c2 = reflectionApi.getClass();

        Class c3 = ReflectionApiExample.class;

Now we have objects so we can get the meta data.
 See below example for getting the meta data of a class ,variable ,method and constructor

public class ReflectionApiExample implements Serializable {

    /* declaring variables */
    static int value = 1234567;
    static String nameOftheclass = "ReflectionApiExample";
    public static int value2 = 98765;
    private static int value3 = 98765;

    /* declaring the methods */
    public void gettingMetadata(String name) {
        System.out.println("getting meta data of method example method");

    public String gettingMetaInfo() {
        System.out.println("getting meta data of method example method");
        return null;

    /*declaring contractor*/
    public ReflectionApiExample(int no) {

    public ReflectionApiExample() {

    public static void main(String[] args)
            throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException {

        Class c3 = ReflectionApiExample.class;

        System.out.println("***** getting meta data of class***** ");
        /*getting the meta data of a class*/
        System.out.println("getting meta data of class");


        Class[] ListOfInterfaces = c3.getInterfaces();

        for (Class c : ListOfInterfaces) {

        /*getting the meta data of a class*/
        System.out.println("***** getting meta data of variables*****");
        int modifier = c3.getModifiers();
        Field[] variables = c3.getDeclaredFields();

        for (Field field : variables) {

            int modifier1 = field.getModifiers();


        /* getting meat date of method*/
        System.out.println("***** getting meta data of methods*****");
        Method[] methods = c3.getDeclaredMethods();
        for (Method method : methods) {


            int method1 = method.getModifiers();

        /*getting meta data of constructor*/
        System.out.println("***** getting meta data of constructor *****");
        Constructor[] constructors = c3.getDeclaredConstructors();
        for (Constructor constructor : constructors) {


            int constructor1 = constructor.getModifiers();


            Class[] parameter = constructor.getParameterTypes();
            System.out.print("Constrctor parameter types :");
            for (Class parameter1 : parameter) {
                System.out.println(parameter1.getName() + " ");


You can down load the above program from attachment given below.

About author

User Image

Seeking a challenging position, where I will be a valuable team member, contributing quality ideas and work for an organization where there is an ample scope to grow for an individual as well as organization growth in Software Development.



Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.