我读过一些帖子,内容如下: “Java 常量是静态最终变量”
我不太明白为什么会这样。为什么将其标记为最终版本还不够?
为什么我们需要添加“static”修饰符?如果它是接口(interface)内的最终字段,那么它已经是不可变的,并且在所有实现类之间共享。
如果有人能为我解决这个问题,我将不胜感激。
最佳答案
由于接口(interface)存储常量,因此变量被声明为public static final
。
现在,它相当直观:
- 常数值不会改变,因此是最终值。
- 所有实现该接口(interface)的类都必须只有一个常量值。因此
静态
- 和
public
因为每个人都必须有权访问常量值。
这是 specification 的内容说:
Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.
关于java - 为什么Java中的接口(interface)变量是隐式静态的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19346817/