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

后端 未结 3 1896
既然无缘
既然无缘 2020-12-03 14:26
interface A {

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


        
相关标签:
3条回答
  • 2020-12-03 14:35

    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

    0 讨论(0)
  • 2020-12-03 14:51

    How about something like this.

    public interface A<T> {
    
         interface B<T> extends A<T>{
    
           T foo(); 
        }
    
    }
    
    0 讨论(0)
  • 2020-12-03 15:00

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

    interface A<T> {
    
        interface B<T> {
           T foo(); 
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题