What is reflection API? How are they implemented

Showing Answers 1 - 4 of 4 Answers

naresg

  • Jun 9th, 2005
 

reflection pkg is used mainlyfor the purpose of getting the class name. 
by useing the getName method we can get name of the class for particular application

  Was this answer useful?  Yes

Arundathi

  • Jul 1st, 2005
 

Reflection is a feature of the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program.

  Was this answer useful?  Yes

Hari

  • Jul 21st, 2005
 

Reflection is the process of introspecting the features and state of a class at runtime and dynamically manipluate at run time.This is supported using Reflection API with built-in classes like Class,Method,Fields,Constructors,etc.,

  Was this answer useful?  Yes

Sam

  • Aug 13th, 2007
 


Reflection - Dynamic Introspection of class

(In other words : Dynamically discover a class, load it, create instance, explore all methods & fields of the class and finally invoke the class.)

Eg:
Filename: ReflectionDemo.java

   class ReflectionDemo{
     public int   myInt;
     public int get()  {  return myInt; }
     public  void set(int temp) { myInt =temp; }
    }
 
Filename: ReflectionClient.java

  class ReflectionClient
   {
    Class myClass = Class.forName("ReflectionDemo");

    Method[] methods = client.getMethods();
    for(Method m : methods) {   System.out.println( m ); }
     //The above will print all the methods of the loaded class
 
    Field[] fields = client.getFields();
    for(Field f : fields) {   System.out.println( f ); }
    //The above will print all the fields(data members) of the loaded class

 //also there are some more methods to invoke and set some values too. Kindly //explore more
   }


Disadvantages of Reflection:

1) Performance -  very slow - Its good to go by the direct way of creating an instance and accessing the methods via the object.
2) Complex

Word of caution : Use reflection if at all its needed  and if it adds some flexibilty ..


Sam
[Msc Software Engg, PSG Tech, Coimbatore]

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions