Immutable Class In Java

[Solved] Immutable Class In Java | Scala - Code Explorer | yomemimo.com
Question : how to create immutable class in java

Answered by : deepika-sahu

To create an immutable class in Java, you need to follow these general principles:
Declare the class as final so it can’t be extended.
Make all of the fields private so that direct access is not allowed.
Don’t provide setter methods for variables.
Make all mutable fields final so that a field’s value can be assigned only once.
Initialize all fields using a constructor method performing deep copy.
Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference.

Source : | Last Update : Thu, 22 Dec 22

Question : grepper subscription required

Answered by : code-grepper

{"tags":[{"tag":"p","content":"You have reached your max daily Grepper answers. <a href=\"https://www.grepper.com/subscriptions.php\" target=\"_blank\" rel=\"nofollow\">Upgrade to professional </a>to view more Grepper answer today."},{"tag":"p","content":"<a href=\"https://www.grepper.com/api/view_product.php?hl=1&amp;pid=42\" target=\"_blank\" rel=\"nofollow\">Upgrade To Grepper Professional</a>"}]}

Source : | Last Update : Mon, 27 Mar 23

Answers related to immutable class in java

Code Explorer Popular Question For Scala