12

I am fairly certain that the code below is valid Java 1.8 code:

import java.util.function.Consumer;

public class UnsolvableClass {

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

    public UnsolvableClass() {
        return;
    }

}

Now, you might be thinking: "Why do you have a return in the constructor?" Well, that's because it's minimal example code that I have come up with that produces this error.

Take note that it is perfectly legal to have a return statement in a constructor.

javac spits this error out when compiling it.

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

I like how it thinks the error is happening on the "return" line when the error has to do with... the "theErrorVariable" variable not being initialized.

If I remove the "return" line, everything compiles fine. Also, if I remove the lambda statement, it also compiles fine.

Note: I have tried compiling this using Eclipse's java compiler and it compiles successfully.

Sounds like a very obscure bug in javac's lambda support. Am I missing something?

If I'm not and this is an actual bug, where would I go to report this to Oracle?

Bradley Odell
  • 1,248
  • 2
  • 15
  • 28
  • 1
    Just wondering: whats the point of having a default constructor only containing a return? Luring people into telling you "that aint valid"? ;-) – GhostCat Sep 15 '16 at 07:25
  • 7
    @GhostCat We finally have a MVCE; don't complain. – shmosel Sep 15 '16 at 07:28
  • 1
    @Nick Tell the fathers of the Java language. Until 4 minutes ago, I probably didn't know that this is possible in the first place. Probably worth a 1+ as reading that question actually widened my knowledge! – GhostCat Sep 15 '16 at 07:32
  • 6
    @Nick It's perfectly legal for a constructor to `return;`, as with any void method. The fact that you may not approve is irrelevant to the question. – shmosel Sep 15 '16 at 07:32
  • 1
    @Nick of course it is not the same. In you example you have got type mismatch, so of course it causes an error. Code posted by the author is totally legal, you can use return statement in constructor as well as you can define field of type Consumer. – ruhungry Sep 15 '16 at 07:33
  • 1
    @shmosel I didn't complain. I was told often enough that people **notice** when I **complain**. Besides, a trailing smiley un-complains everything. – GhostCat Sep 15 '16 at 07:33
  • 4
    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. https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.8 section 8.8.7) - Thus, you have found a bug in that JDK version and should probably file a bug report over at bugs.java.com. – mtj Sep 15 '16 at 07:36
  • For the record: using IBM java8, javac gives me: two errors: `UnsolvableClass.java:5: error: ')' expected private Consumer func = (Object >>>here<<< theErrorVariable) -> {};` and `UnsolvableClass.java:5: error: ';' expected private Consumer func = (Object theErrorVariable >>>) here<<< -> {};` – GhostCat Sep 15 '16 at 07:36
  • 3
    Probably a bug, because compiles fine with the non-lambda equivalent: `private Consumer func = new Consumer() { @Override public void accept(Object theErrorVariable) { } };` – PeterMmm Sep 15 '16 at 07:46
  • @mtj I submitted a bug report. Thanks. – Bradley Odell Sep 15 '16 at 08:26
  • This is reproducible in JDK1.8.0_101 also, and remains the same even if you drop the "Object" as the type for the function input argument, since that is superfluous. Very likely a bug in JDK. – Amrinder Arora Sep 16 '16 at 15:05

1 Answers1

0

This appears to be a Java bug: https://bugs.openjdk.java.net/browse/JDK-8077667

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135