File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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: 42371
    
  64
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
 
wood burning stoves
 
subject: immutable