Why is there no Constant feature in Java?
Solution 1:
Every time I go from heavy C++ coding to Java, it takes me a little while to adapt to the lack of const-correctness in Java. This usage of const
in C++ is much different than just declaring constant variables, if you didn't know. Essentially, it ensures that an object is immutable when accessed through a special kind of pointer called a const-pointer When in Java, in places where I'd normally want to return a const-pointer, I instead return a reference with an interface type containing only methods that shouldn't have side effects. Unfortunately, this isn't enforced by the langauge.
Wikipedia offers the following information on the subject:
Interestingly, the Java language specification regards const as a reserved keyword — i.e., one that cannot be used as variable identifier — but assigns no semantics to it. It is thought that the reservation of the keyword occurred to allow for an extension of the Java language to include C++-style const methods and pointer to const type. The enhancement request ticket in the Java Community Process for implementing const correctness in Java was closed in 2005, implying that const correctness will probably never find its way into the official Java specification.
Solution 2:
What does const
mean
First, realize that the semantics of a "const" keyword means different things to different people:
-
read-only reference - Java
final
semantics - reference variable itself cannot be reassigned to point to another instance (memory location), but the instance itself is modifiable -
readable-only reference - C
const
pointer/reference semantics - means this reference cannot be used to modify the instance (e.g. cannot assign to instance variables, cannot invoke mutable methods) - affects the reference variable only, so a non-const reference pointing to the same instance could modify the instance - immutable object - means the instance itself cannot be modified - applies to instance, so any non-const reference would not be allowed or could not be used to modify the instance
- some combination of the the above?
- others?
Why or Why Not const
Second, if you really want to dig into some of the "pro" vs "con" arguments, see the discussion under this request for enhancement (RFE) "bug". This RFE requests a "readable-only reference"-type "const" feature. Opened in 1999 and then closed/rejected by Sun in 2005, the "const" topic was vigorously debated:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4211070
While there are a lot of good arguments on both sides, some of the oft-cited (but not necessarily compelling or clear-cut) reasons against const
include:
- may have confusing semantics that may be misused and/or abused (see the What does
const
mean above) - may duplicate capability otherwise available (e.g. designing an immutable class, using an immutable interface)
- may be feature creep, leading to a need for other semantic changes such as support for passing objects by value
Before anyone tries to debate me about whether these are good or bad reasons, note that these are not my reasons. They are simply the "gist" of some of the reasons I gleaned from skimming the RFE discussion. I don't necessarily agree with them myself - I'm simply trying to cite why some people (not me) may feel a const
keyword may not be a good idea. Personally, I'd love more "const" semantics to be introduced to the language in an unambiguous manner.