aspose file tools
The moose likes Beginning Java and the fly likes immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "immutable" Watch "immutable" New topic
Author

immutable

Madhu Sudhana
Ranch Hand

Joined: Apr 16, 2006
Posts: 127
How can we make a class immutable?


"And the trouble is, if you don't risk anything, you risk even more." -- Erica Jong.
Shoumin Li
Ranch Hand

Joined: Jul 06, 2007
Posts: 85
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 ]

Java Blog
Madhu Sudhana
Ranch Hand

Joined: Apr 16, 2006
Posts: 127
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
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
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.


Android appsImageJ pluginsJava web charts
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: immutable
 
Similar Threads
Is the Math class considered immutable?
Wrapper Classes
Immutable classes
Wrapper Classes
string vs stringbuffer