JDK-8043179 : Lambda expression can mutate final field
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2014-05-14
  • Updated: 2018-12-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Duplicate :  
Relates :  
Description
The Definitely Unassigned analysis fails to detect the error in a lambda expression initializing a blank final field.

public class LambdaFieldInit {

    private final String x;

    public LambdaFieldInit() {
        Runnable r1 = () -> x = "hi";
        x = "abc";
        r1.run();
        System.out.println(x);
    }

    public static void main(String... args) { new LambdaFieldInit(); }
}

Expected: compiler error in the lambda body's assignment to 'x'
Actual: compiles, prints "hi" at runtime

It needs to be specified that 'x' is not DU in the body of the lambda; currently, that specification is missing.  See JDK-8043176.