• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error accessing outer class field from lambda expression

 
Darryl Burke
Bartender
Posts: 5131
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code compiles.
On executing the code, however, I get an error I've never seen before.
What am I missing?

Note: I'm not looking for a workaround; the actual use case was for a MouseListener#mouseClicked(...) in an generified extension of JLabel, easily solved by adding the MouseListener in each subclass.

edit Another, more general, workaround is to pass the field as a parameter to the inner class's constructor. In the example, that would entail providing a String parameter to the Sub constructor and passing text as the value. That executes without errors.
 
Darryl Burke
Bartender
Posts: 5131
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Playing around with this some more, if the lambda in the super call is replaced with a traditional anonymous inner class, it runs without error.
Is that evidence of a compiler bug? Aren't the two versions -- lambda / inner class -- supposed to be semantically the same?
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may have been fixed in Java 8u20. From the release notes (http://www.oracle.com/technetwork/java/javase/8u20-relnotes-2257729.html):
Area: tools/javac
Synopsis: javac crashes when mixing lambdas and inner classes

The rest of the description does not match this issue, but the title is promising. I'll have to test this later.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid this bug still exists. I get the exact same error message with Java 8u20.
 
Darryl Burke
Bartender
Posts: 5131
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, thank you for checking. FWIW I've already submitted a bug report and the auto acknowledgement has Review ID: JI-9014386.

Looking at the bug parade for javac 8, I don't think this one will be considered important enough to take up.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic