Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final Objects .....

 
Shailesh Vohra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

If references are marked final,

final Object o1 = new Object();
Object o2 = new Object();

we cant do this,

o1 = o2; //This is wrong.

but surely we can change the state of o1 (i.e change the value of instance variables for o1).


Now suppose i want to restrict even changing the state of the object o1.

How can we achieve this..??

Any help is appreciated.



 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shailesh Vohra,
UseCodeTags
And
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)
Or
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.
 
Rajeev Trikha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do a search on Singleton design pattern.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajeev Trikha wrote:Do a search on Singleton design pattern.

Singleton pattern has nothing to do with the original question...
 
Shailesh Vohra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.?
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic