wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 41060
    
  43
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.


Ping & DNS - my free Android networking tools app
 
jQuery in Action, 2nd edition
 
subject: immutable
 
Similar Threads
string vs stringbuffer
Immutable classes
Wrapper Classes
Wrapper Classes
Is the Math class considered immutable?