JDK-8166115 : 'return' in constructor with a lambda assigned to a class field
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • CPU: x86
  • Submitted: 2016-09-15
  • Updated: 2019-08-02
  • Resolved: 2016-12-19
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 10.0.10586]

A DESCRIPTION OF THE PROBLEM :
The given source code fails to compile even though it is completely valid source code according to the Java Language Specification (JLS). The JLS 1.8 states explicitly: "A return statement (��14.17) may be used in the body of a constructor if it does not include an expression." (ref. docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.8 section 8.8.7)

If the 'return' is removed from the constructor then the source compiles.
If the lambda statement is removed then the source compiles.

The combination of a 'return' in the constructor and a lambda assigned to a class field causes the source to fail to compile.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try to compile the given source code.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The source code to compile successfully, without errors.
ACTUAL -
The code does not compile and produces this error:

UnsolvableClass.java:10: error: variable theErrorVariable might not have been initialized
        return;
        ^
1 error

ERROR MESSAGES/STACK TRACES THAT OCCUR :
UnsolvableClass.java:10: error: variable theErrorVariable might not have been initialized
        return;
        ^
1 error

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.function.Consumer;

public class UnsolvableClass {

    private Consumer<Object> func = (Object theErrorVariable) -> {};

    public UnsolvableClass() {
        return;
    }

}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Do not use any 'return' statements in the constructor.
Or don't use lambdas.


Comments
Closing as duplicate of JDK-8077667
19-12-2016

Please see if this is a duplicate of JDK-8077667 (very likely) and needs backport.
19-09-2016

This issue is only observed in 8 and it doesn't exist in 9 Below is the result == 8u112 ea b-06 -sh-4.1$ /opt/java/jdk1.8.0_112/bin/javac UnsolvableClass.java UnsolvableClass.java:8: error: variable theErrorVariable might not have been initialized return; ^ 1 error == 8u102 -sh-4.1$ /opt/java/jdk1.8.0_102/bin/javac UnsolvableClass.java UnsolvableClass.java:8: error: variable theErrorVariable might not have been initialized return; ^ 1 error == 8uxx - Fail (Including 8u112 ea b-06) 9 ea b-135 - Pass
15-09-2016