Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why const keyword is not used in Java? [duplicate]

Tags:

java

Possible Duplicate:
Why is there no Constant keyword in Java?

Why const keyword is not used in Java?
Can you see any disadvantages of using some transitive const or immutable keyword in Java syntax or why common cumbersome approach was chosen?

Can you see reasons for closing the request, does Sun provides any explanations?

like image 767
Mike Avatar asked Sep 15 '11 09:09

Mike


People also ask

Does const exist in Java?

Java does not have const – it instead has final , which can be applied to local "variable" declarations and applies to the identifier, not the type. It has a different object-oriented use for object members, which is the origin of the name.

Is const a reserved word in Java?

Although reserved as a keyword in Java, const is not used and has no function. For defining constants in Java, see the final keyword.

What is the equivalent of const in Java?

The Java equivalent of const In a language such as C++, the const keyword can be used to force a variable or pointer to be read-only or immutable. This prevents it from being updated unintentially and can have advantages when it comes to thread-safety.

What are the keywords not used in Java?

The keywords const and goto are reserved, even though they are not currently used. true , false , and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.


1 Answers

Can you see reasons for closing the request, does Sun provides any explanations?

Yes. Sun provided 3 reasons for why they won't act on the request in the request itself. I quote:

"There are no current plans to add this feature to Java. In addition to creeping featurism, we see the following problems with this feature:

  1. Adding const is too late now. Had this been added from 1.0, the situation could have been different.

  2. Const pollution: the C++ approach requires all const methods to be marked with a const keyword. This means that most methods will have to be marked const explicitly. This tend to clutter all methods in C++.

  3. Compatibility is a very important feature of the JDK. Arguably, the collection classes should be modified to indicate that the elements are const. That would require all existing implementations to be updated in the same way, effectively breaking all existing non-JDK implementations of the collection interfaces. Similarly, hashCode would have to be const, breaking the current implementation of String."


UPDATE

Out of curiousity, I spent a few minutes trawling through the subject lines of the Project COIN mailing list. Somewhat to my surprise, nobody bothered to suggest const. (Or if they did, I missed it.)

So either nobody cares (enough) for this idea any more, or people with sufficient expertise to formulate a project COIN proposal recognize that there's no chance that it would pass muster.

like image 148
Stephen C Avatar answered Oct 04 '22 05:10

Stephen C