Get specific fields
import java.lang.reflect.Field;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
GetFields object = new GetFields();
Class clazz = object.getClass();
// Get all object accessible public fields.
Field[] fields = clazz.getFields();
System.out.println("Number of fields = " + fields.length);
for (Field field : fields) {
System.out.println("Field name = " + field.getName());
System.out.println("Field type = " + field.getType().getName());
}
Field field = clazz.getField("id");
System.out.println("Field name = " + field.getName());
System.out.println("Field type = " + field.getType().getName());
}
}
class GetFields {
public Long id;
protected String name;
private Date birthDate;
Double weight;
}
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