Field (java.lang.reflect.Field) :
- Implements Member Interface
- Provides information about, and dynamic access to, a Single field ( also for static fields )
- Provides access and modification ( set, get ) methods
import java.util.ArrayList;
import java.util.Date;
public class GetFieldsExample {
private int i = 10;
public String strname;
Long longField;
protected Date date;
ArrayList list;
public static void main(String[] args) {
GetFieldsExample test = new GetFieldsExample();
Class objClass = test.getClass();
Field[] fields = objClass.getDeclaredFields();
// The getDeclaredFields returns all type of fields of the Object
for (Field field : fields) {
System.out.println("************************");
System.out.println("Field Name : " + field.getName());
System.out.println("Field Type : " + field.getType().getName());
}
// To get specific Field name
try {
Field field = objClass.getField("strname");
System.out.println("************************");
System.out.println("Field Name : " + field.getName());
System.out.println("Field Type : " + field.getType().getName());
} catch (NoSuchFieldException e) {
System.out.println("NoSuchField : "+e.getMessage());
}
}
}