Get all Declared Fields
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Main{
public static void main(String[] args) {
String name = "java.util.Date";
try {
Class cl = Class.forName(name);
Class supercl = cl.getSuperclass();
System.out.println("class " + name);
System.out.println("Its methods:");
printFields(cl);
System.out.println();
} catch (ClassNotFoundException e) {
System.out.println("Class not found.");
}
}
public static void printFields(Class cl) {
Field[] fields = cl.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
Class type = f.getType();
String name = f.getName();
System.out.print(Modifier.toString(f.getModifiers()));
System.out.println(" " + type.getName() + " " + name + ";");
}
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Reflection Field:
- Get all fields
- Get all Declared Fields
- Get annotations for a Field
- Get "public static final" field
- Get specific fields
- Get Field value by field name
- Get fields for super class
- Get Inherited Methods and fields
- Get Type of the field or return type of a method.
- Field modifiers: isSynthetic, isEnumConstant
- Set field value
- Set null to a field value
- Set private field value