156

I was trying to identify the reason behind constants in Java I have learned that Java allows us to declare constants by using final keyword.

My question is why didn't Java introduce a Constant (const) feature. Since many people say it has come from C++, in C++ we have const keyword.

Please share your thoughts.

1
  • 3
    There is a const keyword, but there is no underlying feature. Corrected your title and tags accordingly.
    – user207421
    Mar 7, 2016 at 2:54

8 Answers 8

156

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.

6
  • 11
    Java's final is similar, though. Jun 20, 2014 at 12:10
  • 70
    No it isn't. final method for example work completely different from C++ const methods.
    – dom0
    Nov 15, 2014 at 10:52
  • 10
    @reinierpost The final keyword on properties or variables just ensures that a property or variable is only assigned to once. One could still alter the state of this object by, for example, calling some method with side effects. final is somewhat similair to stack allocation of C++ in terms of refering to an object rather than a pointer, but that's all it is. This is ofcourse in addition to what dom0 already said.
    – Tim
    Mar 28, 2016 at 18:22
  • 10
    final in Java seems to work like C++ const for value types, but more like a C++ non-const T& for reference types Aug 18, 2016 at 11:28
  • 1
    final is a schizophrenic keyword. While it prevents reassignment, it's also used to expose variables to closures. I might want to prevent reassignment but not expose those variables, but there's no way to do it. It's a rather poorly thought out language feature in my opinion. Sep 12, 2018 at 0:21
89

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.

2
  • 2
    +1 just for the second part of your answer. Many many keywords have non-trivial semantics. Is volatile so simple to understand? Or final? Meh.
    – einpoklum
    Oct 12, 2015 at 19:04
  • OTOH, Java was designed to use as few of those non-trivial features as possible. And I'm not saying that they achieved that goal (or that Java hasn't drifted away from that goal). But excluding it for that reason may still have had merit. That there are other complicated things is all the more reason not to introduce more (or you'd end up with the D language). Feb 6, 2020 at 22:34
7

const in C++ does not mean that a value is a constant.

const in C++ implies that the client of a contract undertakes not to alter its value.

Whether the value of a const expression changes becomes more evident if you are in an environment which supports thread based concurrency.

As Java was designed from the start to support thread and lock concurrency, it didn't add to confusion by overloading the term to have the semantics that final has.

eg:

#include <iostream>

int main ()
{
    volatile const int x = 42;

    std::cout << x << std::endl;

    *const_cast<int*>(&x) = 7;

    std::cout << x << std::endl;

    return 0;
}

outputs 42 then 7.

Although x marked as const, as a non-const alias is created, x is not a constant. Not every compiler requires volatile for this behaviour (though every compiler is permitted to inline the constant)

With more complicated systems you get const/non-const aliases without use of const_cast, so getting into the habit of thinking that const means something won't change becomes more and more dangerous. const merely means that your code can't change it without a cast, not that the value is constant.

14
  • 3
    const int x = 42; - x is a constant
    – anon
    Apr 29, 2010 at 8:39
  • 2
    @Neil If you have one object or variable aliased by both const and non-const pointers, then the value of the const alias can be changed by the non-const alias. Therefore const does not mean a value is constant. It means that the client of a value is constrained not to mutate it. In your example there is no alias, so all users are under the same constraint. This is not the case in general. const effects the clients, not the value - it says you can't change it, not that it won't change. Apr 29, 2010 at 14:11
  • 1
    Const-correctness is about what the programmer should do, not what they can do. I think you all seem to have got that point. Just wanted to add a couple of cents that could be of interest to the causal reader: Design patterns like the immutable interface and immutable object is another way (beatable with cast and reflection) to mimic const in Java. "True" const can be done with SealedObject, alas it destroys the use case of our object. Feb 20, 2013 at 8:01
  • 8
    One should note that the outcome of your program is undefined. const_cast is not there to change const variables, it is there to pass const variables to APIs that are not const correct, but also do not modify the value. I think the habit of thinking that something const won't change is a good one because if they do change, that means your program contains hacks that might break at any time depending on the compiler used.
    – Cygon
    Feb 10, 2015 at 9:16
  • 3
    The claim that it outputs 42 then 7 is wrong; the behavior is undefined. The key is that x was declared const (so, for example, it could be placed in read-only memory). In this example, it would be valid for the compiler to completely ignore the assignment and print 42 twice. If x had been declared without const and been passed as a const-reference to another function, the const_cast would be legal.
    – SJL
    May 2, 2017 at 19:24
7

This is a bit of an old question, but I thought I would contribute my 2 cents anyway since this thread came up in conversation today.

This doesn't exactly answer why is there no const? but how to make your classes immutable. (Unfortunately I have not enough reputation yet to post as a comment to the accepted answer)

The way to guarantee immutability on an object is to design your classes more carefully to be immutable. This requires a bit more care than a mutable class.

This goes back to Josh Bloch's Effective Java Item 15 - Minimize Mutability. If you haven't read the book, pick up a copy and read it over a few times I guarantee it will up your figurative "java game".

In item 15 Bloch suggest that you should limit the mutability of classes to ensure the object's state.

To quote the book directly:

An immutable class is simply a class whose instances cannot be modified. All of the information contained in each instance is provided when it is created and is fixed for the lifetime of the object. The Java platform libraries contain many immutable classes, including String, the boxed primitive classes, and BigInte- ger and BigDecimal. There are many good reasons for this: Immutable classes are easier to design, implement, and use than mutable classes. They are less prone to error and are more secure.

Bloch then describes how to make your classes immutable, by following 5 simple rules:

  1. Don’t provide any methods that modify the object’s state (i.e., setters, aka mutators)
  2. Ensure that the class can’t be extended (this means declaring the class itself as final).
  3. Make all fields final.
  4. Make all fields private.
  5. Ensure exclusive access to any mutable components. (by making defensive copies of the objects)

For more details I highly recommend picking up a copy of the book.

1
  • 5
    const in C++ is MUCH more flexible than full-scale immutability. In a sense, 'const' can be seen as 'immutable within this particular context'. Example: I have a class that isn't immutable, BUT I want to ensure that it is not modified via certain public APIs. Making an interface (and returning it for that public API) as suggested by Gunslinger47 achieves the same thing in Java, but boy - it IS ugly (and so - it is ignored by most of Java developers, leading to significant unnecessary mess)... Oct 26, 2017 at 5:58
4

The C++ semantics of const are very different from Java final. If the designers had used const it would have been unnecessarily confusing.

The fact that const is a reserved word suggests that the designers had ideas for implementing const, but they have since decided against it; see this closed bug. The stated reasons include that adding support for C++ style const would cause compatibility problems.

-1

There is a way to create "const" variables in Java, but only for specific classes. Just define a class with final properties and subclass it. Then use the base class where you would want to use "const". Likewise, if you need to use "const" methods, add them to the base class. The compiler will not allow you to modify what it thinks is the final methods of the base class, but it will read and call methods on the subclass.

2
  • Can you provide some example of this, please? Jan 31, 2017 at 16:10
  • class MYString implements GetString { private final String aaaa; public String getString(); } class MutableString implements GetString { private String aaaa2; public String getString(); public String setString() } Feb 3, 2017 at 13:54
-1

You can use static final to create something that works similar to Const, I have used this in the past.

protected static final int cOTHER = 0;
protected static final int cRPM = 1;
protected static final int cSPEED = 2;
protected static final int cTPS = 3;
protected int DataItemEnum = 0;

public static final int INVALID_PIN = -1;
public static final int LED_PIN = 0;
4
  • Downvoted for rumor-based optimization because I heard a rumor that this is an ineffective strategy.
    – afarley
    May 2, 2019 at 18:11
  • I removed the rumor from the answer. you can still use static final int to create const style code.
    – hamish
    Jul 1, 2019 at 19:29
  • Ok, I have removed my downvote. Maybe some of the other downvotes are about the switch statement (what does it have to do with the rest of your example?)
    – afarley
    Jul 2, 2019 at 6:11
  • in the switch statement I used the cRPM as if it was a const. quite right, considering the above. so yes i have removed the switch.
    – hamish
    Jul 2, 2019 at 18:36
-2

There would be two ways to define constants - const and static final, with the exact same semantics. Furthermore static final describes the behaviour better than const

2
  • @Bozho, you said better behaviour than Const, what way it is? can you share any example
    – gmhk
    Apr 30, 2010 at 2:28
  • well, the variable is static (not belonging to particular instance) and final - cannot be changed.
    – Bozho
    Apr 30, 2010 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.