Immutable Method in Java
- by Chris Okyen
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?