Immutable Method in Java

Posted by Chris Okyen on Programmers See other posts from Programmers or by Chris Okyen
Published on 2012-11-12T01:14:47Z Indexed on 2012/11/12 11:20 UTC
Read the original article Hit count: 372

Filed under:
|
|
|

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?

© Programmers or respective owner

Related posts about java

Related posts about immutable