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

Why ListIterator Has add Method But Iterator Does Not Have in Jav ...

What is the Use of Run Time Polymorphism in Java ?

How Will You Create a Shallow Copy of a Map in Java ?

How Will You Include a Static File in a JSP Page ?

Why We Cannot Create a Generic Array in Java ?