16

I tried to compile the following code:

public interface Foo<T> {
    public interface Bar {
        public void bar(T t);
    }

    void foo(T t);
}

But I get this error: "Foo.this cannot be referenced from a static context."

Specifically, I get it on the "T" in bar(T t). However foo(T t) does not produce the same error. I don't understand why that's a static context and what the error really means.

Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135

1 Answers1

12

A "nested" interface (Bar in your example) is implicitly static. So it can't access instance specific information related to Foo, such as its generic type.

See for example JLS #8.5.1:

A member interface is implicitly static

assylias
  • 321,522
  • 82
  • 660
  • 783