Ads Sponsored By Google :)

Monday, 28 January 2013

using|use of reflection api|class|package in java|java2s with example program|code|tutorial interview question


reflection in java reflection in java examples reflection api in java reflection in java with example reflection in java with example program using reflection in java use of reflection in java reflection api in java example
reflection in java tutorial reflection package in java reflection example in java reflection api in java with example reflection class in java reflection class in java with example reflection in java with example code
reflection in java examples java2s reflection api in java tutorial reflection api in java reflection api in java interview question reflection in java interview question


Reflection: (java.lang.reflect.*;):

               Reflection is a concept of java, which is used to find the internal details of class or interface of java. The internal detail’s is also called METADATA of a class or interface.  The METADATA of class or interface are also called reflective information of class or interface. A class contains some variables, constructors, methods base class some things class implements interface class contains modifiers, so finding all these details is nothing but reflection.

          In JDK, there is tool given called javap (java profiler), it is used for getting internal details of a class or interfaces or interface. To get reflective information, first of all class or interface must be loaded into JVM. Whenever a class or interface is loaded then JVM separates a data into object and METADATA into another object. The object which contains METADATA will be used to get internal details of class or interface.

The Following represents reflection API:

       1)      Loading A Class or Interface in JVM
               To load a class or interface in java into JVM at runtime , we got a method called java forName(). forName() method accepts fully qualified class name as a parameter and loads it into JVM at runtime. forName() is static method given in Class class is class name given in java.lang package.

       2)      Finding Super Class of A Class:
               Whenever a class is loaded into A JVM along with that class, it is super class is also loaded into JVM. Whenever a class is loaded using forName method then we will get a class object by using it we can get a class object of its super class by calling getSuperclass(). To get Name of class from class object, we call getName().

       3)      Finding Constructors:
              Every java class should contain atleast one constructor if we do not add constructor 
    in java class then java compiler adds public default constructor into a java class. To get    
    constructors META DATA, we need to call getConstructors it  returns an array of type    
    constructor.

       4)      Finding Fields of Class:
    A field is nothing but an instance variable in java we can create variables either in a class 
 or in a method. We call variables either in a class or in a method. We call variable declared 
 in a class as instance variable and variable declared in a method are called local variables. 
  By using reflection it is possible to find instance variable META DATA(fields META DATA).

      In java one class can implement any number of interfaces so we say that java supports multiple inheritance interface level. Once the class is loaded into JVM then we can get the META DATA objects of its interfaces by calling getInterfaces().
       While loading a class into JVM,  a long with class its super class its super class its implemented interfaces are also loaded into JVM automatically.

     6)      FindingMethods:
         When a class is loaded into JVM, JVM creates method object to store META DATA of
  a method to get the method objects of a class, we need to call getMethods(). When we call   
  getMethods then internally an array of type method will be created and stores all methods      
  objects finally JVM returns that method array.

    7)      Finding Modifiers of Class:
    Once a class is loaded into JVM we can get its modifiers by calling getModifiers(). When
 we call getModifiers() it returns an encoded integer constructed by JVM for modifiers of class. To decode encoded integer, we need to call methods of modifiers class. Modifier is a class of java.lang.reflect package all declaring methods of modifier class are static methods. So we can call these decoding methods directly with respect to class name. All decoding methods of modifier class returns Boolean.



No comments:

Post a Comment

LinkWithin