In C++, a member method can be const if it does not modify the class. For example:
class Foo {
public:
float getValue() const;
};
Is there something similar I must do in Java classes? How exactly does consting work in Java? (Aside from adding the final keyword before a member variable declaration and initializing it)
Thanks
Although Java lacks a const qualifier, you can achieve some of the same ends with read-only interfaces and immutable types.
So, instead of qualifying a reference as const, you could specify its type to be a read-only interface type.
For example, given these two interfaces:
Then this method’s argument is similar to a const-qualified pointer/reference in C++.