title: java generic object construction within an anonymous subclass
body: <p>I was playing around with anonymous subclasses and I found a problem that I can't get around.</p>
  
  <p>EDIT: problem is solved thanks to thomas, the complete working code is on pastebin (<a href="http://pastebin.com/4GgMRYyi" rel="nofollow">Foo.java</a>; <a href="http://pastebin.com/nGjRkWdg" rel="nofollow">FooTest.java</a>)</p>
  
  <p>2 Classes; Foo and FooTest...
  Code first:</p>
  
  <pre><code>class Foo {
  
      public Foo () {}
  
      public void whichClassAmI () {
          System.out.println(this.getClass());                                                 
      }
  
      public void say () {
          System.out.println("Foo says: nothing");                                             
      }   
  
      public &lt;T extends Foo&gt; T getAnotherFoo () {                                              
  
          try {                                                                                
              Class&lt;? extends Foo&gt; c = this.getClass();                                        
              T r = (T)(c.getConstructor().newInstance());                                     
              return r;                                                                        
          } catch (Exception e) {                                                              
              throw new RuntimeException(e);                                                   
          }
      }                                                                                        
  
  }
  </code></pre>
  
  <p>_</p>
  
  <pre><code>class FooTest {                                                                              
  
      public static String stuff = "";                                                         
  
      public static void main (String[] args) {                                                
  
          Foo f1 = new Foo();                                                                  
  
          // instance of anon subclass                                                         
          Foo f2 = new Foo(){                                                                  
              public void say () {                                                             
                  System.out.println("Modded Foo says: " + stuff);                             
              }                                                                                
          };                                                                                   
  
          f1.whichClassAmI();                                                                  
          f2.whichClassAmI();                                                                  
  
          stuff = "stuff";                                                                     
          f1.say();                                                                            
          f2.say();                                                                            
  
          Foo f3 = f1.getAnotherFoo();                                                         
          f3.say();                                                                            
  
          Foo f4 = f2.getAnotherFoo(); // &lt;-- exception here
      }
  
  }
  </code></pre>
  
  <p>So this code compiles with an unsafe operation warning, runs and throws an exception; output is:</p>
  
  <pre><code>Note: Foo.java uses unchecked or unsafe operations.
  Note: Recompile with -Xlint:unchecked for details.
  class Foo
  class FooTest$1
  Foo says: nothing
  Modded Foo says: stuff
  Foo says: nothing
  Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchMethodException: FooTest$1.&lt;init&gt;()
      at Foo.getAnotherFoo(Foo.java:20)
      at FooTest.main(FooTest.java:23)
  Caused by: java.lang.NoSuchMethodException: FooTest$1.&lt;init&gt;()
      at java.lang.Class.getConstructor0(Class.java:2723)
      at java.lang.Class.getConstructor(Class.java:1674)
      at Foo.getAnotherFoo(Foo.java:17)
      ... 1 more
  </code></pre>
  
  <p>what I don't understand is:</p>
  
  <ol>
  <li><p>f2 class is FooTest$1 and this class seems to be not extending Foo</p></li>
  <li><p>if (1) is true why does " Class c = [...] " can be set with FooTest$1</p></li>
  <li><p>if (1) is false and (2) works correct, why doesn't it find the method?</p></li>
  </ol>