JDK-8051958 : Cannot assign a value to final variable in lambda
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8u20
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2014-07-24
  • Updated: 2015-02-23
  • Resolved: 2014-07-29
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.
JDK 8 JDK 9
8u40Fixed 9 b26Fixed
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_20-ea"
Java(TM) SE Runtime Environment (build 1.8.0_20-ea-b23)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b22, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [versie 6.1.7601]

EXTRA RELEVANT SYSTEM CONFIGURATION :
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
Maven home: D:\apache-maven-3.0.5\bin\..
Java version: 1.8.0_20-ea, vendor: Oracle Corporation
Java home: c:\Program Files\Java\jdk1.8.0_20\jre
Default locale: nl_NL, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"

A DESCRIPTION OF THE PROBLEM :
As registered at https://jira.codehaus.org/browse/MCOMPILER-228
Code example compiles in Eclipse, but not with Maven Compiler Plugin. Code is like below (inside a lamda expression)

final x;
if (some condition)
x = a;
else if (some other condition)
x = b;
else 
x = c;



ERROR MESSAGES/STACK TRACES THAT OCCUR :
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project MediaIndexer: Compilation failure: Compilation failure:
[ERROR] /C:/MAPS/MediaIndexer/src/main/java/example/FinalExample.java:[11,13] cannot assign a value to final variable compareTo
[ERROR] /C:/MAPS/MediaIndexer/src/main/java/example/FinalExample.java:[14,13] cannot assign a value to final variable compareTo
[ERROR] /C:/MAPS/MediaIndexer/src/main/java/example/FinalExample.java:[17,13] cannot assign a value to final variable compareTo

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package example;

import java.util.Comparator;

public class FinalExample {
    public static final Comparator<String> comparator = (o1, o2) -> {
        final String desc1 = o1;
        final String desc2 = o2;
        final int compareTo;
        if (desc1 == null) {
            compareTo = -1;
        }
        else if (desc2 == null) {
            compareTo = 1;
        }
        else {
            compareTo = desc1.compareTo(desc2);
        }
        if (compareTo == 0) {
            return 3;
        }
        return compareTo;
    };
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
The workaround is to remove final for the variable and just ensure that the value is assigned in each code path.


Comments
Note: if the minimal test case above is rewritten so that the lambda is used inisde a method, the bug does not occur. It seems like final analysis doesn't cope well with the lambda being declared in a field context.
28-07-2014

Minimal test case: class Test { Runnable r = ()-> { final int x; if (true) { x = 1; } }; } gives: error: cannot assign a value to final variable x x = 1; ^ 1 error
28-07-2014