This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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

Immutable classes  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the basic steps to create Immutable classes ?

1) Declaring the class as final so as to avoid subclassing and overriding
2) Declaring the accessor methods for the instance variables
3) Making the instance variables private.

Is that all.Anything i missed ?
[ December 25, 2008: Message edited by: Juva Yuva ]
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a good link that i bumped into.
 
Juva Yuva
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Juva Yuva:
What are the basic steps to create Immutable classes ?

1) Declaring the class as final so as to avoid subclassing and overriding
2) Declaring the accessor methods for the instance variables
3) Making the instance variables private.

Is that all.Anything i missed ?

[ December 25, 2008: Message edited by: Juva Yuva ]



Thanks Harvinder.
Correcting my steps ( for other beginners to read)

1) Making all the instance variables final and private.
2) Declaring and implementing public accessor methods for the instance variables.
3) In the accessor method (get) , sending the copy of the instance variables by creating clone , so to avoid the reference being used outside to change the state of it.
4) Declaring the class as final so as to avoid subclassing and overriding (optional, so as to provide Mutable functionality by subclassing )
[ December 26, 2008: Message edited by: Juva Yuva ]
 
Marshal
Posts: 59786
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to check the clone methods of all your fields carefully, that they will actually produce real copy variables. There is no need to clone or copy immutable classes.
It is quite important to make the class final, too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!