Questionable compilation failure

Peter Levart peter.levart at gmail.com
Thu Sep 19 15:11:17 PDT 2013


On 09/18/2013 06:34 PM, Remi Forax wrote:
> On 09/18/2013 06:18 PM, Millies, Sebastian wrote:
>> Hello there,
>>
>> on my Windows 7 (64bit) box, the following does not compile with the developer preview build (b 106). Should it?
>>
>> import java.util.function.Supplier;
>>
>> public class SupplierTest {
>>
>>       class Concrete<T> implements Supplier<Concrete<T>> {
>>           @Override
>>           public Concrete<T> get() {
>>               return this;
>>           }
>>       }
>>
>>       interface Test  {
>>           default void doSomething() {
>>                new Concrete<String>();   // <-----
>>           }
>>       }
>>
>> }
>>
>> The error message is:
>>
>> \SupplierTest.java:16: error: non-static variable this cannot be referenced from a static context
>> new Concrete<String>();
>> ^
>>
>> However, a default method implementation is not static, or is it? (Perhaps it's just the error
>> message that's confusing me.)
> It's maybe a bug, at least the error message is misleading.

I think it's not a bug, but a consequence of the fact that nested 
interfaces are by default static.

public class SupplierTest {
     interface Test {
...

actually means:

public class SupplierTest {
     static interface Test {


It can't be any other way, since interfaces don't have state. Non-static 
nested (inner) classes always have implicit state - the captured 
reference to outer instance.


Regards, Peter

> You declare Concrete as a *non-static* inner class, so it needs an
> instance of SupplierTest
> to be able to create an instance of Concrete.
>
> here, new Concrete<String>() is transformed to this.new Concrete<String>(),
> so you create to create a Concrete with an instance of Test and not with
> an instance of SupplierTest.
>
> I suppose that you just forget to declare Concrete static ?
>
> cheers,
> Rémi
>
>
>
>
>> n  Sebastian
>>
>>
>>
>>
>> Software AG - Sitz/Registered office: Uhlandstra?e 12, 64297 Darmstadt, Germany - Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com
>>
>>



More information about the lambda-dev mailing list