40

Why are Java constants declared static?

class Foo {
    static final int FII = 2 ;
}

In this example I understand the use of final, But why does it have to be static? Why should it be a class variable and not an instance variable?

3
  • 10
    Why would you want more than one instance to have its own copy of FII?
    – Bart Kiers
    Nov 11, 2011 at 11:22
  • 1
    I can declare final int FII = 2; just fine.
    – Jon Lin
    Nov 11, 2011 at 11:24
  • 1
    @JonLin: You can, but you shouldn't since it doesn't add value but only increases memory usage. Exception would be initialization of the constant in the constructor call - then each instance could have it's own constant value (constant after the constructor run) and could have a value like 'constructionTime' or similar. Aug 2, 2015 at 8:22

4 Answers 4

115

If a constant is not static, Java will allocate a memory for that constant in every object of the class (i.e., one copy of the constant per object).

If a constant is static, there will be only one copy of the constant for that class (i.e., one copy per class).

Therefore, if the constant has only one value, it should declared static.

If the constant might have different value for each object, for example the creation time of the object, it should not be declared static.

7
  • 1
    "If the constant might have different value for each object" then by definition it is not a constant.
    – Jezor
    Jan 31, 2020 at 10:57
  • 2
    @Jezor No, it's a constant. It's ok for objects of the same class to have the same constant with different values. " for example the creation time of the object".
    – Rain
    Mar 26, 2020 at 5:36
  • I see. Calling it a "constant" might be technically correct but sounds rather strange. With this, every immutable object declared final is a constant, no matter the scope.
    – Jezor
    Apr 2, 2020 at 13:39
  • @Rain Actually, as per java specs, A variable of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28), is called a constant variable. Constants during creation of the objects beat the purpose of Compile time
    – sss
    Apr 24, 2020 at 7:16
  • @SameerSinha It doesn't matter if a constant value was defined at compile time or run time, that doesn't change the fact that it's a constant(a value that cannot be changed after it's been initialized). For example, in PHP language we have const and we have define one will be evaluated at compile time and the other at run time but most importantly they both create a constant.
    – Rain
    Apr 24, 2020 at 7:59
45

If it could vary by the instance of a class, then it's clearly not a constant. What would it mean to get a different value of pi for each instance of Math (not that Math even allows instances to be constructed)? Or a different case insensitive ordering for each instance of String?

14

It is simply so that you can access them without an instance of that class.

Requiring that an instance be created just to access constant fields is a bit of a waste of resources.

3

Why are java constants declared static ?

Technically they aren't, JLS defines constants as final constant expression (which are referred to but not formally in java as compile time constant expression). meaning a variable declared final which is initialized with a constant expression, meaning without static - https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.4

A constant variable is a final variable of primitive type or type String that is initialized with a constant expression.

But the specification does not address anything regarding Constant Types which java is all about, Classes and Objects - so Jon Skeet answer regarding Math example is missing one part, Math class is considered constant because you cannot instantiate it by making its constructor private.

http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/Math.java -

public final class Math {


    /**

     * Don't let anyone instantiate this class.

     */

    private Math() {}

    public static final double PI = 3.14159265358979323846;

}

To support the Type itself being constant you need to make sure its state is also constant, but only for its members who are mutable and are exposed to changes from outside the type.

For example the literal PI is made public so it's available from outside the type. So to make sure it wont be changed from the outside it is made final, and also static so it will be part of the Class Class<Math> instance and could be exposed for use outside Math without the class being (explicitly) instantiated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.