auser auser - 1 month ago 11
Java Question

Java Generics: non-static type variable T cannot be referenced from a static context

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?

Answer

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