Reflection API in JAVA 2
java.lang.Class class
Class is a final class in java.lang package which extends Object class. Instance of this class represents classes and interfaces in a running Java application. It is used to analyze and change dynamic behaviour of a class at runtime.
Some Important Methods of java.lang.Class class
This class defines several methods using which we can get information about methods, constructors, modifiers and members of a class at runtime.
forName( )
This method takes fully qualified name of classes or interface as its argument and returns instance of the class assocaited with it.
Syntax
static Class< ?> forName(String className)
Example using forName() method
class Student{}
class Test
{
public static void main(String args[])
{
Class c = Class.forName("Student");
System.out.println(c.getName());
}
}
Output : Student
getConstructors( ) and getDeclaredConstructors( )
getConstructors() method returns array of Constructors object that represent all the public constructors of the invoking object. Remember, this method only returns public constructors. If you want to see all the declared constructors of a class then use getDeclaredConstructors(). Following is the general syntax of both,
Constructor< ?>[] getConstructors();
Constructor< ?>[] getDeclaredConstructors();
Example using getConstructors() and getDeclaredConstructors() method
import java.lang.reflect.*;
class Student
{
public Student(){}
public Student(String name){}
}
class Test
{
public static void main(String args[])
{
try
{
Class c = Class.forName("Student");
Constructor< Student>[] ct = c.getConstructors();
for(int i=0; i< ct.length; i++)
{ System.out.println(ct[i]); }
Constructor< Student>[] cdt = c.getDeclaredConstructors();
for(int i=0;i< cdt.length;i++)
{ System.out.println(cdt[i]);}
}
catch(Exception e)
{ e.printStackTrace();}
}
}
Output :
public Student()
public Student()
Student(java.lang.String)
getMethods( ) and getDeclaredMethods( )
getMethods() method returns array of Method object that reflect all the public method of invoking object. getDeclaredMethods() returns only the declared methods of the invoking class object.
Syntax for both is following,
Method< ?>[] getMethods();
Method< ?>[] getDeclaredMethods();
Example using getDeclaredMethods() method
import java.lang.reflect.*;
class Student
{
public void show(){}
void display(){}
}
class Test
{
public static void main(String args[])
{
try
{
Class c = Class.forName("Student");
Method md[] = c.getDeclaredMethods();
for(int i=0; i< md.length; i++ )
{ System.out.println(md[i]); }
}
catch(Exception e)
{ e.printStackTrace();}
}
}
Output :
public void Student.show()
void Student.display()
getFields( ) and getDeclaredFields( )
getFields() returns an array containing Field objects reflecting all the accessible public members of the class or interface represented by this Class object. getDeclaredFields() returns array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
Field< ?>[] getFields();
Field< ?>[] getDeclaredFields();
Example using getFields() and getDeclaredFields() method
import java.lang.reflect.*;
class Student
{
public String name;
int roll;
}
class Test
{
public static void main(String args[])
{
try
{
Class c = Class.forName("Student");
Field ff[] = c.getFields();
for(int i=0; i< ff.length; i++)
{ System.out.println(ff[i]); }
Field f[] = c.getDeclaredFields();
for(int i=0;i< f.length; i++)
{ System.out.println(f[i]); }
}
catch(Exception e)
{ e.printStackTrace();}
}
}
Output :
public java.lang.String Student.name
public java.lang.String Student.name
int Student.roll
Dinesh Kumar S is a 23-year-old System Administrator who enjoys playing games, listening to music and learning new technology. He is friendly and generous, but can also be very lazy and crazy.
Share this
Learn-JAVA