Java does not support const-qualification of references to objects.
Workarounds include the following:
- Immutable objects.
- Unmodifiable views of modifiable objects (for example, those returned by Collections.unmodifiable*()).
- Read-only interface types
- Defensive copies.
Immutable objects prevent modification by the recipient, but they also prevent modification through any reference. They are not equivalent to C++ const pointers/references.
Closest are read-only interface types. You could map a const-correct C++ class to two Java units, one an interface with only the const-qualified methods.
This is sometimes useful, to control what can modify an instance of the class. However, it's not done as frequently as in C++.
answered 2011-10-25T20:02:14.353