title: Using reflection to invoke method on field
body: <p>My code looks like the following:</p>
  
  <pre><code>class MyObject {
  
      MyField f = new MyField();
  
  }
  
  class MyField {
      public void greatMethod();
  }
  </code></pre>
  
  <p>Is there a way to invoke the <code>greatMethod()</code> using reflection on a object of the class <code>MyObject</code>?</p>
  
  <p>I tried the following:</p>
  
  <pre><code>Field f = myObject.getClass().getDeclaredField("f");
  Method myMethod = f.getDeclaringClass().getDeclaredMethod("greatMethod", new Class[]{});
  myMethod.invoke(f);
  </code></pre>
  
  <p>But instead it is trying to call <code>greatMethod()</code> on my myObject directly and not on the field f in it. Is there a way to achieve this without need to modify the MyObject class (so that it would implement a method which calls the appropriate method on f).</p>