• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do i make my class immutable?  RSS feed

 
Gaurav Saxena
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
First of all i dont know whether this question shud be posted here or in some other forum. My apologies in advance if this is not the forum for this question.
Coming back to my point. the questions are
1. Why do we need the String objects to be immutable?
2. Can i have a class (say MyClass) and make it immutable? If yea then can any one throw some light on how this can be done?
Thanx in advance
Gaurav
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer is that making String objects immutable makes Java programming a lot safer. A common problem in Java is when there are multiple references to an object, possibly in multiple threads, and one reference is used to alter the object without the others being aware of it. These concerns can be overcome with synchronization and defensive copies and the like, but they are eliminated completely by making the object immutable. Example:

In the above code, we know that we've mapped the String "key1" to an object, so we can forget about the variable involved, s. If we were able to change s, then we would need to link the variable with the HashMap anytime we needed to retrieve the object. A litttle convoluted, granted, but just an example of the kind of error that might occur.

In order to make any class of your own immutable, just don't provide any methods that allow the member variables to be changed (except for in the constructor). Ensure all the member variables are private, or if not, they should be final.
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fletcher Estes:

In order to make any class of your own immutable, just don't provide any methods that allow the member variables to be changed (except for in the constructor). Ensure all the member variables are private, or if not, they should be final.


Well, actually, that's not good enough. Imagine:



Looks immutable but it is not. There are 2 problems:


This will make it REALLY immutable:


I strongly recommend you read the book "Hardcore Java"
by Robert Simmons, Jr.

Enjoy, Petr
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yes, the method you give as an example would be allowing the user to change a member variable. Didn't I say: "just don't provide any methods that allow the member variables to be changed" ???
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fletcher,

I have to take exception to you code example. You can use any String object whose value is "key1". So the fact that Strings are immuttable is inmaterial in your example.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Petr Blahos:
This will make it REALLY immutable:
A bit more efficient would beObviously this is immutable iff the objects you've put in "inData" are...

- Peter
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also the link at http://faq.javaranch.com/view?ImmutableObject (and feel free to edit the page to add a description or other usefull information).
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rovas Kram:
Fletcher,

I have to take exception to you code example. You can use any String object whose value is "key1". So the fact that Strings are immuttable is inmaterial in your example.


If Strings wreen't immutable in my example, and the user tries to call any methods on the 'i' returned from the HashMap, then a NullPointerException would be thrown (in my example, the last line could be 'System.out.println(i.toString()'). How immaterial is that?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fletcher Estes:


If Strings wreen't immutable in my example, and the user tries to call any methods on the 'i' returned from the HashMap, then a NullPointerException would be thrown (in my example, the last line could be 'System.out.println(i.toString()'). How immaterial is that?


Though you are correct in intent, your example simply is wrong.

First, and most importantly, s = "key2" wouldn't change the old object, it would still just let the reference point to a new one.

Second, and as an aside, System.out.println(null) doesn't throw a NPE, it simply prints "null".
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


Though you are correct in intent, your example simply is wrong.

First, and most importantly, s = "key2" wouldn't change the old object, it would still just let the reference point to a new one.

Second, and as an aside, System.out.println(null) doesn't throw a NPE, it simply prints "null".


You've missed the whole point. Obviously the example is fine in Java
But I was showing the consequences if Strings were not immutable. The contents of the original variable would have been changed. That's what "mutable" means. Do you understand?

And, yes, System.out.println(null) is fine, but did you read my last post? Clearly not.
 
Gaurav Saxena
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit more efficient would be
code:
--------------------------------------------------------------------------------

public class MyImmutableClass { private final List data; MyImmutableClass(final List inData) { data = Collections.unmodifiableList(new ArrayList(inData)); } public List getData() { return data; }}

--------------------------------------------------------------------------------

Obviously this is immutable iff the objects you've put in "inData" are...

- Peter

Hi peter
Are you trying to say that if the List <b>inData</b> contains objects that are mutable then MyImmutableClass will not really be immutable?
Please clarify
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes; when people talk about immutable objects then they usually mean the object and everything it contains. For example, if there was a way to change the contents of the underlying char[] object in a String, we would never consider Strings "immutable".

- Peter
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!