(This is not a philosophical answer but more of a practical one). The requirement for static
modifier is obvious which has been answered by others. Basically, since the interfaces cannot be instantiated, the only way to access its fields are to make them a class field -- static
.
The reason behind the interface
fields automatically becoming final
(constant) is to prevent different implementations accidentally changing the value of interface variable which can inadvertently affect the behavior of the other implementations. Imagine the scenario below where an interface
property did not explicitly become final
by Java:
public interface Actionable { public static boolean isActionable = false; public void performAction(); } public NuclearAction implements Actionable { public void performAction() { // Code that depends on isActionable variable if (isActionable) { // Launch nuclear weapon!!! } } }
Now, just think what would happen if another class that implements Actionable
alters the state of the interface variable:
public CleanAction implements Actionable { public void performAction() { // Code that can alter isActionable state since it is not constant isActionable = true; } }
If these classes are loaded within a single JVM by a classloader, then the behavior of NuclearAction
can be affected by another class, CleanAction
, when its performAction()
is invoke after CleanAction
's is executed (in the same thread or otherwise), which in this case can be disastrous (semantically that is).
Since we do not know how each implementation of an interface
is going to use these variables, they must implicitly be final
.