Question

I know that constants are those variables whose values cannot be changed, but if no part of the program changes their value, are they still required to be declared final? And it also seems that they must be static. Why is that?

Was it helpful?

Solution

You are actually asking several questions at once which I am trying to answer.

Why use constants at all?

Constants are used to avoid magic numbers/strings in your code. If you have a string that appears in several occasions of your code, once you have to change that string you only need to change the constant definition and not every occurrence of the string in your code. Also if a constant is only used once it is often a good thing because of its better visibility.

The final keyword.

Its purpose (at least in this context) is twofold. One is to make it impossible to a programmer to change the value. You might have forgotten that it is a constant. The other is to tell the compiler that the value cannot change at runtime. This can be used to create optimized bytecode (e.g. the constant could be removed and every occurrence replaced by its value by the compiler).

The static keyword.

In Java everything is a Class. And every Class can have several instances (objects). If you dont mark your constant as static then every object has "its own constant". Since you dont want that it makes sense to mark it as static. Static fields (or methods) do exist only once per class (as opposed to once per object of the class).

OTHER TIPS

It is certainly possible to declare non-static finals:

class Employee {
  final String empId;

  public Employee(String empId) { this.empId = empId; }
}

In other cases you want the field to be constant across all instances of the class:

class Color {
  final static int BLACK = 0xFFFFFF;
}

As to why you want to declare them final at all instead of just not changing them ever,

  • It increases program readability, it tells the reader of program something about its behavior that would otherwise have to be in documentation
  • Compiler reminds if you attempt to change it by mistake

Because static belongs to class rather than any instance.

When it is static single copy shared across all the instances. Where as instance member have the individual copy.

consider you need to increase/decrease game score (count), in each stage (Stage class) of your game.

enter image description here

Normally when you're going to use a constant value on your code, you declare a final static variable. That prevents you from spreading "magic values" around the code, which is not a good practice, for mantainability and legibility reasons.

If you don't declare them final, code made by other people (or you, in case you forget your initial intention) may modify the variable.

If you don't declare them static, every instance of the class you create will have a copy of it, also you'll have to create an instance to get the value. That's not what you want, usually.

We declare constants because we will always need some "magic numbers" that are fixed in the code. Using constants means that they are easier to spot, and that when you change them you will change all of them with a edit.

Imagine that your code defines that your window will show 15 records, and that you will consider people as adults when they are 15 years old. Without constants, changing the size of the windows means that you will have to find the 15 ocurrences, do not miss any, and do not change a 15 that means age by mistake.

The static part is because you do not want to instantiate an object to get a data that is not related to a particular instance (that is exactly what static means, btw, not only when used for constants).

It's not strictly necessary, but it's recommended for reasons of memory-efficiency:

If you don't declare your constant as static every instance of the class (possibly thousands of them) that is created will keep it's own value of (or at least a reference to) that constant in memory, whereas a static member is only kept once per class - and since it's constant anyway, that's sufficient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top