What is reflection?

What is reflection?

Reflection is an API in java that allows an executing Java program to examine or \"introspect\" upon itself, and manipulate internal properties of the program. For example, it\'s possible for a Java class to obtain the names of all its members and display them. A reflection is what you see when you look in the mirror. Reflection is for dealing with class files you know very little about ahead of time. It is a powerful approach to analyze the class at runtime. If new classes are added into your application dynamically then Reflection is used to get the structure of the class. 

Example : 

import java.lang.reflect.*;
 
   public class ShowMethods {
      public static void main(String args[])
      {
         try {
            Class c = Class.forName(args[0]);
            Method methods[] = c.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++)
            System.out.println(methods[i].toString());
         }
         catch (Throwable e) {
            System.err.println(e);
         }
      }
   }

By:Jalees
Date:2016-01-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher