• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

final Objects .....  RSS feed

 
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.



 
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.
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do a search on Singleton design pattern.
 
Sheriff
Posts: 9613
37
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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!