0

I've read posts that said something like: "A java constant is a static final variable"

I don't really understand why this is true. Why isn't marking it as final enough?

Why do we need to add the "static" modifier? If it's a final field inside an interface it's already immutable and is shared among all implementing classes.

I'd appreciate if someone could clear this up for me.

user207421
  • 305,947
  • 44
  • 307
  • 483
matanc1
  • 6,525
  • 6
  • 37
  • 57
  • 2
    You don't need to add the static modifier in an interface. Are you asking the question in your title or the question in your text? – user207421 Oct 13 '13 at 17:22

1 Answers1

2

Since interfaces store constants, the variables are declared public static final.
Now, it is fairly intuitive:

  1. Constant values do not change, hence the final.
  2. All the classes implementing the interface must have only one value for the constant values. hence the static
  3. And public cause everyone must get the access to constant values.

Here is what the specification says:

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.

An SO User
  • 24,612
  • 35
  • 133
  • 221