what is a static interface in java?

拟墨画扇 提交于 2020-07-30 04:20:22

问题


I was reading through the Map.Entry interface, when I noticed it is a 'static' interface. I didn't quite understand what a static interface is, and how is it different from a regular interface ?

public static interface Map.Entry<K,V>

This is the definition of the interface. Docs here: http://docs.oracle.com/javase/6/docs/api/java/util/Map.Entry.html


回答1:


I'm curious about the case when it's not an inner interface.

The static modifier is only allowed on a nested classes or interfaces. In your example Entry is nested inside the Map interface.

For interfaces, the static modifier is actually optional. The distinction makes no sense for interfaces since they contain no code that could access the outer this anyway.




回答2:


Static inner interface and inner interface is the same, all access rules are the same as with inner static class. So inner interface can be accessible only if you have access to its parent class/interface. In case below you will have access to interface B only from package of interface A, because A has default access modifier. BTW: interface B could be static or not.

 interface A {
    void testA();
    public interface B {
        void testB();
    }
 } 



回答3:


Finally, even Android Studio indicates that using static with inner interface is not needed:



来源:https://stackoverflow.com/questions/62779626/useless-static-keyword-in-enclosed-interface

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!