In Java, there is the final
keyword in lieu of the const
keyword in C and C++.
In the latter languages there are mutable and immutable methods such as stated in the answer by Johannes Schaub – litb to the question How many and which are the uses of “const” in C++?
Use const to tell others methods won’t change the logical state of this object.
struct SmartPtr { int getCopies() const { return mCopiesMade; } }ptr1; ... int var = ptr.getCopies(); // returns mCopiesMade and is specified that to not modify objects state.
How is this performed in Java?
1
You can’t. I’m not familiar with Java 7, but at least in Java 6 you cannot tell the compiler a method is not supposed to mutate its arguments or the this
instance.
Unfortunately final
in Java doesn’t mean the same as const
in C++.
A final
argument means something else: merely that you cannot reassign it, so that the following is an error:
A method(final B arg) {
...
arg = something; // error, since arg is final
...
}
This is a good practice, but it won’t prevent you from mutating arg
by calling one of its methods.
A final
method such as final A method(...)
is related to subclassing rules, and not with mutation.
5
Java’s final
is not in lieu of C’s const
. In Java final
may appear in five places. Attribute, variable and formal parameter declarations and class and method definitions. Using final
for attributes, variables or parameters means, you cannot reassign them, so the following will not be accepted by the compiler:
final List<String> xs = new ArrayList<>();
xs = new LinkedList<>(); //illegal
However, note that you can still change the object referenced by xs
:
final List<String> xs = new ArrayList<>();
xs.add("Hello World");
As for classes, final
forbids inheriting from this class. So, this is illegal too:
final class A {
}
class B extends A { //illegal
}
And for methods final
forbids overriding the method in a subclass, thus the following is illegal.
class A {
final void m() {}
}
class B extends A {
final void m() { System.exit(1); } //illegal
}
So why would anyone use one keyword for three different intentions? Well, they are connected. If you take a look at java.lang.String
you will see, that it is declared as final
. And since there are not operations in String
that will change the value of the instance you can safely assume that
final String GREETING = "Hello, World!";
defines a constant.
BTW, const
is a reserved word in Java, too. But it has no semantics and thus cannot be used in any Java program.
The only way you can make a Java object immutable is from within the class of that object itself and not from other calling classes.
You may only have constant pointer (final reference by the official Java name) meaning that you cannot modify the reference to point to another object; you cannot have pointer to constant, meaning you cannot modify the referring object, as you may have in C/C++.
The only way to create const method in Java is to create a const interface for the object. Like:
interface ConstSomething {
int getCopies();
}
class Something implements ConstSomething {
int getCopies() { return mCopiesMade; }
}
Yes, unlike C++ const
this affects performance, because now the method has to be called virtually.
Note, that the same applies to C#.
4