How Will You Make an Object Immutable in Java ?

To make an object immutable follow these two rules. One, do not use any setter methods that can change the fields of your class. Two, make the fields final. By following these rules, the member variables cannot be changed after initialization. This will ensure that member variables of an Object do not change. And thus the Object will be considered Immutable.



You May Interest

What is the Different Type of Tag Library Groups in JSTL ?

What are the Uses of Optional in Java ?

What are the Different Types of Classloaders in Java ?

What is the Difference Between an Iterator and ListIterator in Ja ...

What is the Reason to Organize Classes and Interfaces in a Packag ...