20
interface A<T> {

    interface B {
       // Results in non-static type variable T cannot
       // be referenced from a static context
       T foo(); 
    }

}

Is there anyway round this? Why is T seen as static when referenced from A.B?

auser
  • 6,307
  • 13
  • 41
  • 63

3 Answers3

10

All member fields of an interface are by default public, static and final.

Since inner interface is static by default, you can't refer to T from static fields or methods.

Because T is actually associated with an instance of a class, if it were associated with a static field or method which is associated with class then it wouldn't make any sense

alisianoi
  • 2,003
  • 3
  • 31
  • 46
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    Thanks, that answers the first part of the question. How do I get the type T seen in the inner interface to be the same type at that of the containing interface? – auser Jun 09 '12 at 18:51
  • 2
    I would remove the first sentence as it's unnecessary and misleading - yes interface fields are implicitly `public static final` but this has to do with interface *methods* which are implicitly `public abstract`. The fact that inner interfaces are implicitly `static` themselves is what's important. – Paul Bellora Jun 09 '12 at 22:12
  • well inner interface is same as inner field – jmj Jun 10 '12 at 05:27
3

How about something like this.

public interface A<T> {

     interface B<T> extends A<T>{

       T foo(); 
    }

}
Akhi
  • 2,242
  • 18
  • 24
0

Your inner interface doesn't know what T is. Try this.

interface A<T> {

    interface B<T> {
       T foo(); 
    }

}
Dodd10x
  • 3,344
  • 1
  • 18
  • 27