AIM:
To write a Java program to display constructors, fields and methods using reflection.
ALGORITHM:
Step 1: Start
Step 2: Create an object for the class java.lang.Integer
Step 3: Creatr the objects for constructor, field and method
Step 4: Display the constructors
Step 5: Display the fields
Step 6: Display the methods
Step 7: Stop
SOURCE CODE:
testreflect.java
import java.lang.reflect.*;public class testreflect
{
public static void main(String args[])
{
try
{
Class c=Class.forName("java.lang.Integer");
String name=c.getName();
Constructor[] constructors=c.getConstructors();
Field[] fields=c.getFields();
Method[] methods=c.getMethods();
System.out.println(name+" has "+constructors.length+" constructors");
for(int i=0;i<constructors.length;i++)
System.out.println(""+constructors[i]);
System.out.println(name+" has "+fields.length+" fileds");
for(int i=0;i<fields.length;i++)
System.out.println(""+fields[i]);
System.out.println(name+" has "+methods.length+" methods");
for(int i=0;i<methods.length;i++)
System.out.println(""+methods[i]);
}
catch(ClassNotFoundException e)
{
System.out.println("Class not found");
}
}
}
OUTPUT:
RESULT:
Thus the Java program to display the constructors, fields and methods using reflection was performed and the output was verified.
.
EmoticonEmoticon