Moro Moro - 6 months ago 68
Java Question

Java reflection - access protected field

How can I access an inherited protected field from an object by reflection?


Two issues you may be having issues with - the field might not be accessible normally (private), and it's not in the class you are looking at, but somewhere up the hierarchy.

Something like this would work even with those issues:

public class SomeExample {

  public static void main(String[] args) throws Exception{
    Object myObj = new SomeDerivedClass(1234);
    Class myClass = myObj.getClass();
    Field myField = getField(myClass, "value");
    myField.setAccessible(true); //required if field is not normally accessible
    System.out.println("value: " + myField.get(myObj));

  private static Field getField(Class clazz, String fieldName)
        throws NoSuchFieldException {
    try {
      return clazz.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
      Class superClass = clazz.getSuperclass();
      if (superClass == null) {
        throw e;
      } else {
        return getField(superClass, fieldName);

class SomeBaseClass {
  private Integer value;

  SomeBaseClass(Integer value) {
    this.value = value;

class SomeDerivedClass extends SomeBaseClass {
  SomeDerivedClass(Integer value) {