aspose file tools*
The moose likes Java in General and the fly likes immutability in presence of arraylist as an instance variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "immutability in presence of arraylist as an instance variable" Watch "immutability in presence of arraylist as an instance variable" New topic
Author

immutability in presence of arraylist as an instance variable

Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
I have a question about immutability of a class.
How can I ensure the immutability of a class when an arraylist is one of the member variable in it?
Lets say we have a getter method for a member variable for which the type is ArrayList. In that case when I get a reference to that variable, I can add or remove an element from it. In that case immutability seems to get violated.
Can somebody please explain this concept in details?

Thanks in advance.


Regards,
Mahesh Kedari - Fidus Technologies Ltd.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mahesh Kedari wrote:I have a question about immutability of a class.
How can I ensure the immutability of a class when an arraylist is one of the member variable in it?


If you mean "deep" immutability--that is not even being able to modify the objects held by the list if they are mutable--then your only options are:

1) Don't give any access to the list or its elements.

2) Anything that returns the list or any of its elements must return a deep copy.

If you don't care if the list elements themselves get modified, or if they are immutable, then simply calling Collections.unumodifiableList() will give you an unmodifiable copy of the list.

Lets say we have a getter method for a member variable for which the type is ArrayList.


Unrelated to your question, but just as a point worth noting--usually you'd declare your method to return List, not ArrayList.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

There are two ways this is usually solved:
1) Return a copy:
2) Returns an unmodifiable view:
Of course neither option guards against the List elements being mutable. If that's the case you will need to make a copy of the List that contains copies of the elements.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Mahesh Kedari wrote: Lets say we have a getter method for a member variable for which the type is ArrayList. In that case when I get a reference to that variable, I can add or remove an element from it. In that case immutability seems to get violated.

If you are directly returning reference by getter method, then the class is not immutable.

Your getter method must not return the reference to any mutable object. If the object is mutable, create a copy of it (copy of whole object, not just reference - e.g. Collections.copy(arrayList1,arrayList2)) and return it. That way, even if you modify the returned object outside getter method, you'll be modifying a copy of instance variable, and not actual member of the class.

More information can be found here.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Anayonkar Shivalkar wrote:
Your getter method must not return the reference to any immutable object.


You mean mutable object. Returning a reference to an immutable object is perfectly acceptable.

Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Yes, my mistake. I meant mutable. I've corrected that post. Thanks for pointing it out.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Anayonkar Shivalkar wrote:Yes, my mistake. I meant mutable. I've corrected that post. Thanks for pointing it out.


Well, you corrected part of it ... :-)
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5



Corrected completely now
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: immutability in presence of arraylist as an instance variable
 
Similar Threads
Different output when string concatenaion is involved
emergency service required
Serialization Query
Copy an ArrayList
Math class immutable