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?

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
well inner interface is same as inner field
3

How about something like this.

public interface A<T> {

     interface B<T> extends A<T>{

       T foo(); 
    }

}

Comments

0

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

interface A<T> {

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

}

1 Comment

Isn't the T in B<T> a different T than in A<T> here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.