aspose file tools*
The moose likes Features new in Java 8 and the fly likes Error accessing outer class field from lambda expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Features new in Java 8
Bookmark "Error accessing outer class field from lambda expression" Watch "Error accessing outer class field from lambda expression" New topic
Author

Error accessing outer class field from lambda expression

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

I'm afraid this bug still exists. I get the exact same error message with Java 8u20.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

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.
 
 
subject: Error accessing outer class field from lambda expression