4

In java, constants as known as keyword (final) with a value that will never change. I have seen some people create constants without declaring a static modifier. My question is, should constants be declared as a static? If so or if not, why?

TheDetailer
  • 289
  • 5
  • 16

6 Answers6

4

If you assign a value to the final variable when declaring it, there's no point in it not being static, since each instance would have its own variable having the same value, which is wasteful.

However, if you need an instance variable whose value can only be set once (but different instances may have different values), that variable would have to be final but not static.

For example :

class Person 
{
    final int id;
    public Person(int id) {
        this.id = id;
    }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • I do not fully agree with your answer. If my instance of Person has a List/Map, then I usually declare that List as final, but not as static, since the content belongs to the instance of the class. Please correct me if I am missing something here – metters Jul 01 '20 at 13:19
  • 1
    @metters The question was about constants (primitives, Strings and other immutable types), and my answer was that there's no point to define non-static constants (since all of them will always hold the same value). I agree that if you have a final variable that references some mutable instance (such as your List or Map), different instances of the class can mutate that instance in different ways, so my answer does not apply in this scenario. – Eran Jul 01 '20 at 13:28
  • Yes right, they (lists) are not immutable. Thanks a lot! – metters Jul 01 '20 at 14:17
2

You will first need to understand what constants do (i.e, what happens when you mark a field / local variable as final.)

When a primitive / String field is marked as final, it becomes a compile-time constant i.e, its value is passed as part of the bytecode itself. Thus its value is not computed / generated at runtime. This gives you a performance benefit.

The keyword static is used to say - this field is NOT unique for each instance of a class. You could have non-static final constants as well. Also, if a method local variable (primitive) is marked as final, it also becomes a constant.

So, No, static has nothing to do with constants. It is a design choice.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

Constants with the final keyword will never change.. actually you cannot change the instance this field is referencing, but you can change values inside this instance.

Imagine this example:

class SomeClass {
    final Car MYCAR;
    ...
}

With this code you will not be able to change the reference of MYCAR:

MYCAR = new Car(.....);

But you can do something like:

MYCAR.setPrice(10000);

So yes, there is a point in NOT making this field static if any instance of SomeClass needs to have their own copy of the object MYCAR but you don't want anyone to change the reference of this object.

David Herrero
  • 704
  • 5
  • 17
0

Technically, the keyword final is enough for a constant since you can't change the value of final variables once assigned.

static should be used if the constant is not tied to a particular object instance.

For example, consider you have a Circle class, and you have a method to calculate area. You need the constant Pi for this purpose. Pi constant does not change from circle to circle. So it makes sense to declare Pi as a static final.

Amila
  • 5,195
  • 1
  • 27
  • 46
0

Whatever you like. I would personally use static. You don't need to create an object when you declare it static. Also you can make a 'constants' file, where you store all constants like. public final static ...

So you basically use static final if it's a 'constant' used by all objects. If not, just make it final and pass it through the constructor.

kevintjuh93
  • 1,010
  • 7
  • 22
0

When you use keyword static in a class the all instances of class. i.e. All objects of a class share the same variable where as If you declare a class as final the it cannot be instantiated ( it's object cannot be created ). So if you declare a variable final then it can be assigned value only once. Let suppose

class CalculateArea {

    final static double PI = 3.1417;
     /*write rest of the code to calculate area.
     the value of PI will remain constant no matter 
       how many times its object is made
    if you try to override the value of `PI` it will raise an error.
    */
}
oreopot
  • 3,392
  • 2
  • 19
  • 28