File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes immutable object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "immutable object" Watch "immutable object" New topic

immutable object

Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
hi all,
is it possible to make an Object immutable ?? for eg. consider an Object
Object obj = something();
if i call obj.toString() does it make this object immutable ?? becoz toString() returns String and String is immutable right ?? Plz guide me if im wrong..
is there any other way to make an Object immutable ??

SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
Hi Rajeev,
I only have little understanding about your question, hope this help.
Immutable object is object that can't be modified. You can achieve this by making all fields private and make no method that can modify the fields. The fields is only set during the creation of the object.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

I wrote an article on Mutable and Immutable objects for the April newsletter. You could start there
I agree. Here's the link:
subject: immutable object
jQuery in Action, 3rd edition