Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

immutable

 
Madhu Sudhana
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we make a class immutable?
 
Shoumin Li
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I known,these are some rules for making a class immutable:
1. Don't provide any methods that modify the object (known as mutators).
2. Ensure that no methods may be overridden.
3. Make all fields final.
4. Make all fields private.
5. Ensure exclusive access to any mutable components.

[ July 25, 2007: Message edited by: Shoumin Li ]
[ July 25, 2007: Message edited by: Shoumin Li ]
 
Madhu Sudhana
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply.....

Can I assume that String class is following the rules you have mentioned?

or String class has some extra features............?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Madhusudhana E:
Can I assume that String class is following the rules you have mentioned?


Even better: looking at the javadocs of the String class you can find out for sure if these conditions are met.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic