-1

I know that a variable declared like this:

public class Example {
    public static final int MY_CONSTANT = 10;

}

is considered to be a Java constant. I don't really understand why a variable declared only final cannot be considered a constant?

DianaM
  • 257
  • 4
  • 11
  • 3
    _I don't really understand why a variable declared only final cannot be considered a constant_ Are you asking as opposed to not being modified with `static`? Or not initialized at declaration? Can you clarify in which case you don't think it's considered a _constant_? And do you mean _constant_ as unchanging or do you mean _constant_ as in _constant expression_ as defined in the JLS? – Sotirios Delimanolis May 17 '16 at 20:22
  • 2
    I have marked this as a duplicate of the other question, as you are asking exactly the same question, but I must say I find the answers there rather unsatisfying. In particular, the [JLS](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.4) says "A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.28).", which makes no mention of `static`. – Andy Turner May 17 '16 at 20:31
  • @Andy Turner: yes, even local variables can be constants. If you want to have fun, you can use such local constants as `case` label in a `switch` statement or reference them in an annotation of a local `class`… – Holger Jun 03 '16 at 19:35

1 Answers1

1

A variable that is final but not static has a constant value for one object, meaning that it can only be modified by a constructor of that object. But for different objects the variable can still have different values. That's why it is not a constant in a strict sense.

Another reason is that you don't have to create an object to access a static variable. Why should you create an object in order to access a constant?

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
  • 3
    It won't have different values if it is assigned the value when it is declared: `final int myConstant = 10;`. – Andy Turner May 17 '16 at 20:26
  • @AndyTurner: True, but it is not really obvious (within the IDEs I know) how the value has been assigned. For `static final`s it is obvious that they have a constant value within the whole program. – Frank Puffer May 17 '16 at 20:34
  • @FrankPuffer Why is it more obvious? In both cases, you'd have to inspect the code or read the documentation. – Sotirios Delimanolis May 17 '16 at 20:35
  • @FrankPuffer I can think of a way to read two different values from a `(static) final` field - you can actually read them before they are initialized (in specific cases), so you could read the "before" and "after" initialization values. So in pathological cases, they're not constant. – Andy Turner May 17 '16 at 20:35
  • @SotiriosDelimanolis: Most IDEs will show you the type of the variable (`static final` or just `final`) when you select it. – Frank Puffer May 17 '16 at 20:39
  • What does the type have to do with it? An IDE will show the type of any field, if it's accessible. I'm confused by your reasoning. – Sotirios Delimanolis May 17 '16 at 20:41
  • @SotiriosDelimanolis: If the type is `static final`, it is obviously a constant. If it is just `final`, it could have different values for different objects. – Frank Puffer May 17 '16 at 20:50