11

Recently I've found a subtle difference between anonymous class and lambda expression:

public class FinalTest {
    final Runnable x = new Runnable() {
        @Override
        public void run() {
            System.out.println(x.hashCode());
        }
    };

    final Runnable y = () -> System.out.println(y.hashCode()); 
}

Usually lambdas are equivalent to the anonymous classes. Even my Eclipse IDE has the refactoring to convert the x to lambda (it becomes exactly like y) and convert y to anonymous class (it becomes exactly like x). However the lambda gives me a compilation error while anonymous class can be perfectly compiled. The error message looks like this:

>javac FinalTest.java
FinalTest.java:9: error: self-reference in initializer
    final Runnable y = () -> System.out.println(y.hashCode());
                                                ^
1 error

So the question is: why there's such difference?

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • I would, but I don't have JDK 8 on my machine at the moment, which is why I threw it out there. – Ryan J May 08 '15 at 18:09
  • @TagirValeev why don't you post the error message, instead of forcing us to compile the code or guess what the problem can be? – JB Nizet May 08 '15 at 18:09
  • @JBNizet, error message added. – Tagir Valeev May 08 '15 at 18:11
  • 2
    See this report: https://bugs.openjdk.java.net/browse/JDK-8027941 – aioobe May 08 '15 at 18:26
  • 5
    The assumption that "lambdas are equivalent to anonymous classes" is where you started to go wrong. They are similar, but have some significant differences. The biggest difference is the interpretation of names. Anonymous have some horribly complex scoping rules (names could resolve either to the enclosing lexical scope *OR* up the inheritance hierarchy.) Lambdas have far simpler resolution rules; a name not defined in the lambda has exactly the same meaning as in the enclosing lexical context. "x" in your case is shorthand for "this.x", and 'this' in a lambda means then enclosing 'this'. – Brian Goetz May 08 '15 at 21:19

1 Answers1

15

This has to do with JLS #8.3.3 dealing with forward references. In particular, if you use a fully qualified name it compiles (because the third condition of that rule becomes false The use is a simple name in either an instance variable initializer of C or an instance initializer of C):

final Runnable y = () -> System.out.println(this.y.hashCode());

In the case of the anonymous class, the fourth condition (C is the innermost class or interface enclosing the use) is not true because the enclosing class is the anonymous class itself.

assylias
  • 321,522
  • 82
  • 660
  • 783