In the fragment example, a static interface was defined in order to notify activity about clicks:

static interface WorkoutListListener { void itemClicked(long id); }; 

Why is the interface static? For example, if a variable is static, it is used in a single copy for all instances of the class, but what do static interfaces do?

    1 answer 1

    According to JLS 8.5.1. Static Member Type Declarations :

    A member interface is implicitly static ( §9.1.1 ). It is not necessary to specify the static modifier.


    An interface is a member of a class that is implicitly static. When declaring an interface - a member of a class, an excessive indication of the static modifier is allowed.

    Constructions

     public class SomeClass { abstract static interface Interface { public abstract void foo(); } } 

    and

     public class SomeClass { interface Interface { void foo(); } } 

    are equivalent, since the interface is always abstract , the nested interface is always static , and the interface methods are always public . Before java 8, interface methods were always abstract , in the eighth version the interface method can be static or default , in the absence of these modifiers, the abstract implied. Explicit specification of modifiers is permissible, but not welcomed in terms of style.

    Fields in the interface are always public , static and final , the redundant indication of these modifiers is also valid.

    • That is static - in this case, the value does not play? - user189127
    • 2
      @ bukashka101 it will be there anyway, but it can be entered explicitly - zRrr
    • Well ... I mean just an extra word that is written "just like that." - user189127
    • By the way, you will need to read about innovations in the interfaces of Java 8, just in case. Thank :). - user189127