Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldn't use `return` in a constructor of a class which has a lambda field

The code looks like:

import java.util.function.Consumer;

public class Test {
    Consumer<Integer> lambda = key -> {
    };

    public Test() {
        return; // NOTICE THIS LINE
    }
}

the build output would be like:

~\Test.java:7:19
java: variable key might not have been initialized

java -version:

java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

os ver: Microsoft Windows [版本 10.0.18363.1734]

enter image description here

like image 287
正逍遥0716 Avatar asked Sep 03 '21 08:09

正逍遥0716


1 Answers

You have rediscovered the JDK-8077667 bug. As stated in the bug report it was fixed in version 9.

like image 110
Andy Brown Avatar answered Sep 21 '22 00:09

Andy Brown