Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java Reflection and java.lang.Class class

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 183
    Comment on it

    Java Reflection

    The run time behaviour of a class can be examined and modified at run time,this process is called java reflection. To examine and change the run time behaviour of a class  java.lang.Class is used which provides many methods to modify run time behaviour. java.lang.reflect packages provide classes for java reflection. Java reflection API is to be used with debugger and test tools.

    java.lang.Class class

    This class performs two tasks:

    • For examining and changing run time behaviour of class,java.lang.class provides various methods.
    • It also provides methods to get information i.e meta data of a class at run time.

    Few methods of java.lang.Class class are:

    •  public String getName()
    •  public static Class forName(String className)throws ClassNotFoundException
    • public Object newInstance()throws InstantiationException,IllegalAccessException.

    A program to demonstrate method of java.lang.class. This program is using  public String forName() method.

    class Demo{}  
      
    class Test
    {  
     public static void main(String args[])
     {  
      Class c=Class.forName("Demo");  
      System.out.println(c.getName());  
     }  
    }  

    Output:

    Demo

    public String forName() returns the instance of class class i.e the class is loaded dynamically. This method cannot be used with primitive types and only be used when full name of class is known.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: