title: anonymous class as generic parameter
body: <p><BR>
  I want to create a class that gets an object from anonymous class definition to store. I used a generic typed class to achieve that. Then i want to define some operations using functional interfaces that gets this object as a parameter to work with.<BR>
  Code says more than words. So have a look at this:</p>
  
  <pre><code>public class Test&lt;T&gt; {
      @FunctionalInterface
      public interface operation&lt;T&gt; {
          void execute(T object);
      }
      private T obj;
      public Test(T _obj){
          obj = _obj;
      }
      public void runOperation(operation&lt;T&gt; op){
          op.execute(obj);
      }
  
      public static void main(String[] args){
          Test&lt;?&gt; t = new Test&lt;&gt;(new Object(){
              public String text = "Something";
          });
          t.runOperation((o) -&gt; {
              System.out.println(o.text);  // text cannot be resolved
          });
      }
  }
  </code></pre>
  
  <p>My problem is that o.text in the implementation of the functional interface cannot be resolved. Is this some kind of type erasure consequence?<BR>
  The interesting thing is that I can get this code working when I implement the functional interface in the constructor.<BR>
  Have a look at this code:</p>
  
  <pre><code>public class Test&lt;T&gt; {
      @FunctionalInterface
      public interface operation&lt;T&gt; {
          void execute(T object);
      }
      private T obj;
      private operation&lt;T&gt; op;
  
      public Test(T _obj, operation&lt;T&gt; _op){
          obj = _obj;
          op = _op;
      }
      public void runOperation(){
          op.execute(obj);
      }
      public static void main(String[] args){
          Test&lt;?&gt; t = new Test&lt;&gt;(new Object(){
              public String text = "Something";
          }, (o) -&gt; {
              System.out.println(o.text);
          });
          t.runOperation();
      }
  }
  </code></pre>
  
  <p>This works perfect and prints out "Something". But what is wrong with my first approach? I really don't get the problem here.</p>