-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
8
-
x86_64
-
linux
FULL PRODUCT VERSION :
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Linux 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
When a Lambda expression defined as a class instance references a final variable that is initialized only inside a constructor, but without calling "this." before the variable, the compiler throws a compilation error.
With the "this." before the variable, the compiler works fine.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See provided source code to compile.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Compiling 2 source files to /home/bruno/javabug/build/classes
/home/bruno/javabug/src/FooBar.java:14: error: variable foobar might not have been initialized
System.out.println(foobar); // throws compilation error
1 error
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
/**
* @author bruno.borges@oracle.com
*/
public class FooBar {
private final String foobar;
public FooBar() {
foobar = "foobar";
}
private final java.util.function.Function ds = n -> {
System.out.println(this.foobar); // compiles successfuly
System.out.println(foobar); // throws compilation error: "variable foobar might not have been initialized"
return foobar;
};
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The reference to the variable must be preceded with "this." or else the compiler throws a compilation error.
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Linux 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
When a Lambda expression defined as a class instance references a final variable that is initialized only inside a constructor, but without calling "this." before the variable, the compiler throws a compilation error.
With the "this." before the variable, the compiler works fine.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See provided source code to compile.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Compiling 2 source files to /home/bruno/javabug/build/classes
/home/bruno/javabug/src/FooBar.java:14: error: variable foobar might not have been initialized
System.out.println(foobar); // throws compilation error
1 error
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
/**
* @author bruno.borges@oracle.com
*/
public class FooBar {
private final String foobar;
public FooBar() {
foobar = "foobar";
}
private final java.util.function.Function ds = n -> {
System.out.println(this.foobar); // compiles successfuly
System.out.println(foobar); // throws compilation error: "variable foobar might not have been initialized"
return foobar;
};
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The reference to the variable must be preceded with "this." or else the compiler throws a compilation error.
- duplicates
-
JDK-8039026 Definitely unassigned field can be accessed
- Closed
-
JDK-8042754 Can't compile Lambda referencing final variable initialized on constructor
- Closed
- relates to
-
JDK-8043176 16.1.10: Add DA/DU rules for lambda expressions
- Closed