This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi Shailesh Vohra,
I think this is called immutable objects
Might be achieved by,
In every method that changes the state of the object:
Take a copy of the object and make the required changes, then return that newly created object. (Referred String class documentation)
Do not provide any method that changes the state of the object. So only at the time of creation we can set the value for the instance variables of the object.
Shailesh Vohra wrote:i did not understand your explanation Anbarasu Aladiyan.
Can one explain again...?
I just have to make my Objects also final....How to achieve it.?
What does meant by changing object's state?
It means changing the object's instance variable. So first make sure instance variables are not visible by putting private modifier.
And do NOT provide any setter methods. Because changing the instance a variable means changing the state of the object.
And in all the methods that changes the instance variable (for example subtract, add...) of the object.
First takes a copy of current object and make the changes in instance variables of new object. Then return that newly created object.
I think better you can refer string class documentation (source code).
Also be sure you know very well about immutable objects
Here is the concat() of string class: