Ads Sponsored By Google :)

Tuesday 29 January 2013

how can we Load|Loading the a class|interfaces in java using reflection with example


Loading the a class in java using reflection Loading the a class in java with example how can we Load the a class in java using reflection how can we Load the a class in java how can we Load the a class in java using reflection with example Loading the a class in java using reflection with example Loading the interfaces in java using reflection Loading the interfaces in java with example how can we Load the interfaces in java using reflection how can we Load the interfaces in java how can we Load the interfaces in java using reflection with example Loading the interfaces in java using reflection with example

 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.

        Whenever a class is going to load into JVM, forName() method will verify the following two 
     conditions.

           a)      It checks whether  a given class belongs to given package or not.
           b)      It that class does not exist in the given package then forName() throws   
                    ClassNotFoundException.
           c)      If class is found in the given package then again forName() checks whether this class 
                    is already loaded into JVM or not. If loaded then forName method does not the class              
                    once again.
          d)      Once the class is loaded in JVM it separates data and META DATA and stores these 
                   objects into method area and Heap area of JVM.
          e)      META DATA object will be stored in method area and data object will be stored in 
                  Heap area
          f)       forName method return method area object which contains META DATA.
          g)      In order to store the META DATA object we need an object of type class
           Class c=Class.forName(“java.Lang.Exception”);

     Syntax :

            Class c=Class.forName(“Fully qualified Name”);
          h)      In java, to store META DATA objects we use Class.
i)                
          Class is a class of java.lang package and it can store META DATA of any loaded class of 
            java.

    Example :
        
               Class c1=Class.foName(“java.lang.String”);
                Class c2=Class.foName(“java.utilArrayList”);
                Class c3=Class.foName(“Pack1.Demo”);

        In first statement c1 represents META DATA of String class, similarly in second statement 
        c2 represents META DATA of Array List and c3 represents META DATA of Demo.
         Class object is not loaded class object. It is META DATA of Demo.
            forName() is static factory method, because in java if any method produce an object then  

           we call such type of method as factory method.
     Every static method is not a factory method similarly every factory method is not a static 
    method.

  •     what is a factory method and types of factory methods?
    




No comments:

Post a Comment

LinkWithin