title: How to access a field's value in an object using reflection
body: <p><strong>My Question:</strong> How to overcome an <code>IllegalAccessException</code> to access the value of an object's field using reflection.</p>
  
  <p><strong>Expansion:</strong> I'm trying to learn about reflection to make some of my projects more generic. I'm running into an <code>IllegalAccessException</code> when trying to call <code>field.getValue(object)</code> to get the value of that field in that object. I can get the name and type just fine.</p>
  
  <p>If I change the declaration from <code>private</code> to <code>public</code> then this works fine. But in an effort to follow the "rules" of encapsulation I don't want to do this. Any help would be greatly appreciated! Thanks!</p>
  
  <p><strong>My Code:</strong></p>
  
  <pre><code>package main;
  
  import java.lang.reflect.Field;
  
  public class Tester {
  
    public static void main(String args[]) throws Exception {
      new Tester().reflectionTest();
    }
  
    public void reflectionTest() throws Exception {
      Person person = new Person("John Doe", "555-123-4567", "Rover");
      Field[] fields = person.getClass().getDeclaredFields();
      for (Field field : fields) {
        System.out.println("Field Name: " + field.getName());
        System.out.println("Field Type: " + field.getType());
        System.out.println("Field Value: " + field.get(person));
        //The line above throws: Exception in thread "main" java.lang.IllegalAccessException: Class main.Tester can not access a member of class main.Tester$Person with modifiers "private final"
      }
    }
  
    public class Person {
  
      private final String name;
      private final String phoneNumber;
      private final String dogsName;
  
      public Person(String name, String phoneNumber, String dogsName) {
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.dogsName = dogsName;
      }
    }
  }
  </code></pre>
  
  <p><strong>The Output:</strong></p>
  
  <pre><code>run:
  Field Name: name
  Field Type: class java.lang.String
  Exception in thread "main" java.lang.IllegalAccessException: Class main.Tester can not access a member of class main.Tester$Person with modifiers "private final"
      at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
      at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
      at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
      at java.lang.reflect.Field.doSecurityCheck(Field.java:983)
      at java.lang.reflect.Field.getFieldAccessor(Field.java:927)
      at java.lang.reflect.Field.get(Field.java:372)
      at main.Tester.reflectionTest(Tester.java:17)
      at main.Tester.main(Tester.java:8)
  Java Result: 1
  BUILD SUCCESSFUL (total time: 0 seconds)
  </code></pre>