title: Java reflection get sub class variable values / get object instance from member field
body: <p>Say I have a class that has some member variables one or more being another class also with member variables. How can I get the value of each variable in both classes?</p>
  
  <p>I can get the variables by calling: <code>getDeclaredFields();</code> And the values of the Foo Object by calling <code>field.get(obj)</code> then iterating of them again to get the sub class variables. </p>
  
  <p>But how can I get the value for each sub class member variable without an instance of that class?
  My trivial example works because the variables are the same for each instance of the <code>Bar</code> class and I can call <code>innerField.get(new Bar())</code> </p>
  
  <p>I guess it could work if I could somehow get the instance of the object from the field?</p>
  
  <p>Thanks in advance</p>
  
  <p><em>First Object:</em></p>
  
  <pre><code>public class Foo {
      public Foo(){}
      private Integer someInt = 123;
      private Long someLong = 987654321L;
      private String someString = "qwertyuiop";
      private Bar bar = new Bar();
  }
  </code></pre>
  
  <p><em>Second Object:</em></p>
  
  <pre><code>public class Bar {
      public Bar(){}
      private Double someDouble = 123.456;
      private String innerString = "another String";
  }
  </code></pre>
  
  <p><em>main/ calling method:</em></p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      Foo foo = new Foo();
      getMemberFields(foo);
  }
  </code></pre>
  
  <p><em>Getting variables and values:</em></p>
  
  <pre><code>public static void getMemberFields(Object obj) throws IllegalAccessException,               NoSuchFieldException {
      Class&lt;?&gt; objClass = obj.getClass();
      System.out.println("obj: " + obj.getClass());
  
      Field[] fields = objClass.getDeclaredFields();
      for(Field field : fields) {
          field.setAccessible(true);
          System.out.println("Field: " + field.getName() + " value: " + field.get(obj));
  
          Field[] innerFields = {};
          if (!field.getType().getName().contains("java.lang")) {
               innerFields = field.getType().getDeclaredFields();
          }
  
          for (Field innerField: innerFields) {
              if (!innerField.getName().contains("java.lang")) {
                  Object subObj = field.getType();
  
                  innerField.setAccessible(true);
                  System.out.println("");
  
                  System.out.println("Sub Field: " + innerField.getName());
                  System.out.println( "Sub Field value: " + innerField.get(new Bar()));
              }
  
          }
      }
  }
  </code></pre>
  
  <p><em>Output</em></p>
  
  <pre><code>obj: class Foo
  Field: someInt value: 123
  Field: someLong value: 987654321
  Field: someString value: qwertyuiop
  Field: bar value: Bar@61bbe9ba
  Sub Field: someDouble
  Sub Field value: 123.456
  Sub Field: innerString
  Sub Field value: another String
  </code></pre>