Reflection in java

Reflection:-
       Reflection is the way to recognize any class at the runtime even if their name is found at runtime.
If at the development time the class name is not known by the developer then we cann't use that class without reflection.
       The use of class can be load that class, to create the object of that class, invocation of the methods. By Reflection the details of the class can also be obtained such as list of their methods, list of constructors, list of the fields, name of parent class and list of implemented interfaces.

Note:- Reflection is meta data of class.

        According to the Reflection each and every class and members of that class represented by the objects these objects are known as the reflected object or meta object.
        Through the reflection in any java program we can obtained the meta data.

java.lang.Class:-
                This is a class, In the heap area a  separate object of Class class always be created for the each loaded class. That means whenever any class gets loaded immediatly an object of Class class is created for that class.

class loading:-
               when the .class file loaded from the secondary storage into the primary storage within JVM then it will be known as class loading.
               Their are two types of class loading-
       (1) Implicit class loading:-
                                 when the first object of any class is about to be created or first time any static member is invoked by class name.(which ever is earlier) then class gets loaded.
     
             Advantage:- No separate statement will be written for class loading.
            Disadvantage:- we can never load the class implicitly if the name of that class is found at the runtime.
 Example:-
   
class A
{
      /* declaration of static block*/
      static
      {
            System.out.println("A class loaded.");
      }
       A()
       {
            System.out.println("object of A created.");
       }
}
class B
{
      static
      {
             System.out.println("B class loaded.");
      }
      public static void main(String[] args)
      {
              new A();
      }
}

Note:- at the time of class loading following 3 task will be performed-
             (1) Class class object will be created.
             (2) static variables of that class will be created.
             (3) static block of that class will be invoked.

Internal Working of the above program:-
             firstly the B class loaded because it has the main() and Class class object is created for the B class then static block of B is executed and message is print on the console. After that main executes and object of A is about to created but first the A class loaded on the RAM and Class class object is created for the A class then static block of A is executes and then constructor creates the object of A and Class class object of A is referenced to object of A and then main() is refer to object of A.


                    getClass() of java.lang.Object class is used to return the ref. ID of the Class class object.
                                              public Class getClass()
universally this method will be available in all the java class. This method always returns the Class class object of the class of current object.
            toString() of the Class class always returns the name of the loaded class.

change in main():-

public static void main(String[] args)
{
        A ref=new A();
        Class c=ref.getClass();
        System.out.println("loaded class:"+c);
                        //or
       System.out.println("loaded class:"+c.getName());
}
       
           (2) Explicit class loading:-
                                  In this type of class loading we have to write the separate statement in the application. Their is the static method named forName() i.e. used to load the class and returned their Class class object reference.
            public static Class forName(String className) throws ClassNotFoundException
The argumented class name must be fully qualified name that means if class is in the package then class name with package name must be written.

Note:- If argumented class is not found then this method throws the Exception.
example:-(changes in main())
public static void main(String[] args)
{
      try
      {
            Class c=Class.forName("A");
            System.out.println("loaded class:"+c.getName());
      }
      catch(Exception e){}
}

Advantage:- if class is not known at the development time then our program can also load the class by taking their name at run time.

Disadvantage:- A separate statement for loading the class have to be written.

Loading of the class by getting their name at runtime:-

changes in the main():-
 
try
{
       Class c=Class.forName(args[0]);
}
catch(Exception e)
{}

newInstance() of the java.lang.Class class is used to create the object of the loaded class.
               public Object newInstance()
newInstance() always uses the default constructor to create a object.

changes in main():-

try
{
    Class c=Class.forName(args[0]);
    Object ob=c.newInstance();
}
catch(Exception e)
{}

when we create a new object by using newInstance() we have to store the ref. ID of object into known parent class ref. variable.
            newInstance() can throws one of the two types of checked Exception.
            public Object newInstance() throws IllegalAccessException, InstantiationException
-IllegalAccessException encounters when constructor is private.
-InstantiationException encounters when their is no default constructor exist.

Comments

Post a Comment

Popular posts from this blog

Inter-Thread communication

define System.out.println()?