47

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: Map.Entry<K,V>.

brainydexter
  • 19,826
  • 28
  • 77
  • 115

3 Answers3

46

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.

Phil K
  • 4,939
  • 6
  • 31
  • 56
Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
  • Thanks for pointing that out. I was not aware of the fact that Entry is an inner interface and static modifier is only allowed on nested class/interface. – brainydexter Dec 06 '11 at 07:50
  • 18
    "The distinction makes no sense for interfaces"... This is what confuses so many. There's no difference. They shouldn't have allowed the "static" keyword for interfaces. Am I right here? – Ravindranath Akila Jul 29 '14 at 04:06
  • I don't understand that explanation. As I understand static method is method that you can't override, the same about class, if class is static you can't override it methods cause they all is static, but interface can't be static cause in this case you will not be able to override it methods. So I don't understand how interface can be static? – Max Husiv Dec 05 '16 at 13:53
  • 3
    @MaxHusiv that understanding of static classes is incorrect, it does not cause methods to be static. For a nested class `static` means that the class does not have an implicit reference to the outer class. – Jörn Horstmann Dec 05 '16 at 14:37
  • static interfaces in interfaces uses in core hibernate for example interface CustomEntityDirtinessStrategy. – Mikhail Mar 27 '18 at 08:58
4

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();
    }
 } 
Stanislav Levental
  • 2,165
  • 1
  • 14
  • 28
0

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

enter image description here

Mike Keskinov
  • 11,614
  • 6
  • 59
  • 87