July 26, 2024
Banking

Why the Java const keyword is unimplemented


The original architects of the Java language reserved the const keyword, yet the term remains unimplemented and most likely never will be. It’s a situation that causes many developers to wonder why.

What does const in Java even mean?

Anyone who sees 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.

Both C and C++ use the const keyword, but those languages aren’t purely object oriented like Java is, and that belies the crux of the problem.

After all, 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 “is an object only constant under a specific set of 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.

Why is Java’s const keyword unimplemented?

The const keyword is not implemented in Java because Java’s final keyword does a better job of expressing what it means to be a constant in an object-oriented system.

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 Java’s static and final keywords are combined, a class level variable can be made constant, essentially turning it into a global variable.

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 Records. This adds immutable, reference-free objects to the language, and again provide a Java language syntax that implements a concept some might associate with const.

Why does Java even have a const keyword?

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.

const in Java

The const keyword in Java cannot be used without causing a compile time error.

Will const in Java ever be implemented?

Why make const a reserved word if there was no intention of implementing it?

One possible reason is to just avoid confusion.

If const wasn’t a reserved word, developers would be allowed to use the phrase to name variables. Just imagine how much confusion that might cause for C++ and JavaScript developers who learned to program with languages where const has an implementation.

Simply making the const keyword in Java a reserved word helps avoid any confusion.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. View more
Accept
Decline