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?
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?
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?