Is it necessary to declare all immutable objects as final?

This is not necessary. The functionality of achieving immutability can also be achieved by defining the members of a class as private and not providing any setter methods to modify/update the values. Any references to the members should not be leaked and the only source of initializing the members should be by using the parameterized constructor.

We should note that the variables declared as final only takes care of not re-assigning the variable to a different value. The individual properties of an object can still be changed

Author: Susheel kumar

Leave a Reply

Your email address will not be published. Required fields are marked *