[BUG] Missing error? blank final may not have been initialized
Raffaele Sgarro
raffaelesgarro at gmail.com
Mon Apr 7 22:50:12 UTC 2014
Hi Alex,
In fact, the lambda behavior is correct to me. Why do you think it would be
unreasonable for the anonymous?
2014-04-08 0:09 GMT+02:00 Alex Buckley <alex.buckley at oracle.com>:
> I don't believe javac has ever given an error in this case. It would be
> unreasonable to require 'data' to be definitely assigned before the body of
> the anonymous class. OTOH, names in the body of a lambda expression are
> specified as if they appear outside the lambda expression, where 'data'
> would be definitely unassigned, so an error is due there.
>
> Alex
>
>
> On 4/7/2014 10:02 AM, Raffaele Sgarro wrote:
>
>> §16 states:
>>
>> For every access of a local variable or blank final field x, x must be
>> definitely assigned before the access, or a compile-time error occurs.
>>
>> Consider the following code:
>>
>> class Ideone{
>>
>> public interface Provider{ String get(); }
>>
>> public static class Outer{
>>
>> private final String data;
>> private final String token;
>> private final Provider secretProvider= new
>> Provider() {
>>
>> public String get() {
>> return data;
>> }
>> };
>>
>> public Outer() {
>> token= secretProvider.get();
>> data= "FOOBAR";
>>
>> }
>>
>> public String getToken() {
>> return token;
>> }
>>
>> }
>>
>> public static void main(String[] args) throws
>> java.lang.Exception {
>> Outer outer= new Outer();
>>
>> System.out.println(outer.getToken()); // Prints null
>> }
>> }
>>
>> Note that if I used a lambda expression, instead, javac would have
>> complained.
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20140408/688dee6a/attachment.html>
More information about the compiler-dev
mailing list