Interface variables are static and final by default in Java, Why?

JavaObject Oriented ProgrammingProgramming

An interface defines a protocol of behavior and not how we should be implemented. A class that implements an interface adheres to the protocol defined by that interface.

  • Interface variables are static because java interfaces cannot be instantiated on their own. The value of the variable must be assigned in a static context in which no instance exists.
  • The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned. In other words, interfaces can declare only constants, not instance variables.

Template :

interface interfaceName{
   // Any number of final, static variables
   datatype variableName = value;
   // Any number of abstract method declarations
   returntype methodName(list of parameters or no parameters);
}
raja
Published on 23-May-2019 18:42:25
Advertisements