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  

Reflection Field:
  1. Get all fields
  2. Get all Declared Fields
  3. Get annotations for a Field
  4. Get "public static final" field
  5. Get specific fields
  6. Get Field value by field name
  7. Get fields for super class
  8. Get Inherited Methods and fields
  9. Get Type of the field or return type of a method.
  10. Field modifiers: isSynthetic, isEnumConstant
  11. Set field value
  12. Set null to a field value
  13. Set private field value