• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

immutability in presence of arraylist as an instance variable

 
Mahesh Kedari
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20398
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, my mistake. I meant mutable. I've corrected that post. Thanks for pointing it out.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Corrected completely now
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic