| In Java how can we create immutable class?
Below are the some important facts about how to create immutable class
(1)First we declare class as final so that it cannot be extended.
(2)Second we create all fields as private so that direct access is not allowed.
(3)Third we will not provide setter methods for variables.
(4)Fourth create all mutable fields final so that its value can be assigned only once.
(5)Fifth we will initialize all fields via a constructor performing the deep copy.
(6)Sixth we will Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference. | | |