The original architects of the Java language made the ‘const’ keyword a reserved word. But the term remains unimplemented and most likely never will be. It’s a situation that causes many developers to wonder why.
Let’s learn more about the ‘const’ keyword in Java and some alternatives to describe constants in the language.
What does ‘const’ in Java even mean?
Anyone who looks at the ‘const’ keyword in Java would logically assume its originally intention was to define a constant value. That assumption wouldn’t be too far off the mark.
The competing languages C and C++ both the use ‘const’ keyword, but those languages aren’t purely object oriented. Herein lies the problem. What exactly would it mean for something to be a ‘const’ in Java?
- Does being constant mean an instance variable doesn’t change?
- Does being constant mean a class variable doesn’t change?
- Does being constant mean an object’s methods can’t be overridden?
- Does being constant mean an object reference doesn’t change?
- Does being constant mean an object is immutable?
With all the above questions considered, another big one would be if an object be constant under only certain circumstances?
For example, some languages allow ‘const’ to be part of a method signature, which indicates that an object and its properties can’t change for the short period of time in which they are used inside a method. This is sometimes referred to as const-correctness. Language architects can end up down a deep rabbit hole as they examine these quests and try to nail down the meaning of the ‘const’ keyword.
Java’s ‘final’ vs. ‘const’ keywords
Many of the use cases one might initially address with the ‘const keyword in Java are more than adequately addressed using Java’s ‘final’ keyword. In Java, ‘final’ is used to:
- mark a primitive value as being constant;
- indicate a method cannot be overridden;
- specify that an object reference cannot change; and
- ensure a variable is unchanged within a method.
When the ‘static’ and ‘final’ keywords are combined, a class level variable can be made constant. While this doesn’t quite cover all the different scenarios in which the ‘const’ keyword is used in other languages, it comes close. Furthermore, the choice to combine ‘static’ and ‘final’ together provides greater flexibility in how constant properties behave, as opposed to the use of just one keyword that tries to address both instance and class level semantics.
When developers consider the power and flexibility the language provides by allowing the ‘static’ and ‘final’ keywords to be daisy chained together, one might deduce that the reason why the ‘const’ keyword in Java is unimplemented is because it’s not needed. The ‘static’ and ‘final’ keyword combination effectively and arguably provides a more expressive way to declare unchanging variables than would an implementation of the ‘const’ keyword in Java.
In terms of fully immutable objects, the historic work-around has been to declare instance variables private and restrict access through public setters and getters. More recently, Java has introduced the concept of value types as part of JEP 169. This will add immutable, reference-free objects to the language, and again provide a Java language syntax that implements a concept many would associate with ‘const.’
Discussions about using the ‘const’ keyword in Java to implement const-correctness started back in 1999. However, the enhancement proposal was rejected as being feature creep. Language architect assertions that the addition of a const implementation in Java after a full increment — long term support release of the JDK was already out the door — would cause code bloat and potentially backwards compatibility issues completely killed the concept. The idea of implementing the ‘const’ keyword in Java hasn’t been revisited since.
Why is ‘const’ not implemented in Java?
Why make ‘const’ a reserved word if there was no intention of implementing it?
Why is ‘const’ in Java a reserved word without an implementation?
Again, it’s largely to avoid confusion. Other keywords provide equivalent functionality, and because the concept doesn’t fit well in an object-oriented paradigm. Or to put it in layman’s terms, Java doesn’t need it.
For the most part, because the JDK doesn’t need it, ‘const’ has no implementation in Java. Other language features provide the ability to declare constants or provide immutability. The purveyors of the language wanted to avoid confusion and decided that not even well-meaning developers could use the term in their code.