Java interfaces

Why can't interfaces be declared as static?

+3
source share
4 answers

Think of an interface like a plan. it is nothing concrete. Just a diagram of what the class should implement if it sticks to (inherits) the interface.

Java (iirc) does not have the concept of "static" classes as such, that is, "Static" is not a keyword in a class declaration, as in C #. Instead, a static class is a class consisting only of static elements and methods.

As you know, static members and static methods refer to a class, not an instance.

Since an interface is just a project, not a concrete one, a “static” interface does not make sense.

, .

, , , .

+6

-, . ?

-, :

public class SomeClass {
    static interface StaticInterface {
    }
}
+2

: , static interface

: Java .

+2

, , ( , Java 5):

public static (or better final?) interface Colors {
   public final int RED = 1;
   public final int GREEN = 2;
   ...
}

- , (, java_1.4), , .

0

Source: https://habr.com/ru/post/1737891/


All Articles