为什么Java中的接口变量默认是静态和最终的?

为什么Java中的接口变量默认是静态和最终的?

An interface defines a protocol of behavior and not how we should be implemented.
实现接口的类遵循该接口定义的协议。

  • 接口变量是静态的,因为Java接口不能单独实例化。变量的值必须在没有实例存在的静态上下文中分配。
  • final修饰符确保分配给接口变量的值是真正的常量
    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);
}

登录后复制

以上就是为什么Java中的接口变量默认是静态和最终的?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!