This is for Java . Hi Sir , How can we make class immutable . | Core Java Forum
Deepak Kumar Tiwari Posted on 26/10/2020

How can we make class immutable .

Yogesh Chawla Replied on 26/10/2020

These are the steps to be followed.

Declare the class as final so it can't be extended.
Make all fields private and final so the direct access is not allowed.
Don't provide setter methods for variables
Initialize fields via constructor performing deep copy.
Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference.

Rest we have explained everything as part of topic '

Please do check that too.