Java’s Weakest Link

I have seen this every term in my Java track so far. “Is [whatever] like C++ const?” Sorry, no, it’s not. You see, Java doesn’t have anything like C++ const. You might think that final is like const, but it’s not:

  • A final variable in Java can be assigned to only once, but if the variable is a reference-type, you can still change what it refers to. Fun! (Yes, that is what the language-spec says.)
  • A const variable in C++ can be assigned to only once, where it’s declared, and nothing is allowed to change about the value, whether it’s an object or not. Now that is a nice feature!

I would like Java to have an equivalent to C++ const. Some people have actually worked on variants of Java that include const or something like it. (Just search the web and you’ll turn up more than a few!) But, for now, const is just a reserved keyword in Java, waiting for an implementation.

Leave a Reply