10

Why const is a reserved keyword in Java but has no function? If they decided to use final instead then whats the point of having const?

NPE
  • 486,780
  • 108
  • 951
  • 1,012
flav
  • 175
  • 1
  • 2
  • 6
  • 1
    possible duplicate of [Why is there no Constant keyword in Java?](http://stackoverflow.com/questions/2735736/why-is-there-no-constant-keyword-in-java) – Tomasz Nurkiewicz May 31 '12 at 07:59
  • 1
    @TomaszNurkiewicz: To me, that seems like a totally different question (it's about semantics, whereas this one is purely about the keyword). – NPE May 31 '12 at 08:01

6 Answers6

13

From the JLS:

The keywords const and goto are reserved, even though they are not currently used. This may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs.

By way of historical perspective, I can offer you the following quote by Josh Bloch from 2003:

Josh Bloch: We do not have plans to add support for the const keyword to the Java language. It was a mixed blessing in C++; as you know, it's merely advisory, and can cast on or off. Gosling hated it, and did final instead. What you really want is "immutable," but it's a research problem to make this work properly.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Yea ... but see my answer :-) – Stephen C May 31 '12 at 08:06
  • @StephenC: I am no Java historian. However, I don't think Bloch using the word "hate" when describing Gosling's attitude towards `const` is consistent with the latter "keeping his options open". :-) – NPE May 31 '12 at 08:37
  • I think Bloch meant that Gosling hated the way that `const` was implemented in C++ ... not the actual keyword. Besides, Gosling was part of a team. Block saying "it is a research problem to make it work properly" is (IMO) consistent with them making `const` a reserved word to keep their options open ... and not doing the same for `struct`, `union` and so on. – Stephen C May 31 '12 at 08:53
  • @StephenC: Perhaps. At any rate, we both are speculating and will probably never know the actual ex ante thinking. – NPE May 31 '12 at 10:10
3

You would need to ask the pre Java 1.0 designers to find out their original motivation, but I would surmise that they were simply keeping their options open. The goto reserved word is another example.

My guess is that the text that "aix" found in the JLS is a "postfacto" rationalization. Why? Because struct, union, unsigned and so on are NOT reserved words in Java! In short, I don't buy it ... no matter what the JLS claims :-)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

"The keywords const and goto are reserved, even though they are not currently used."

i don't know about the exact background but maybe they used to be part of java?

tagtraeumer
  • 1,451
  • 11
  • 19
1

It is just reserved keyword. It does not have any use case or functionality

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
0

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.

juergen d
  • 201,996
  • 37
  • 293
  • 362
0

From JLS -

"The keywords const and goto are reserved, even though they are not currently used. This may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs"

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37