Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Class with a collection of mutable objects but the elements in the class are not mutable  RSS feed

 
Ranch Hand
Posts: 1174
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am a newbie and I have a question which I am trying to understand,

what would you do if if you have a class with a collection of mutable objects but the elements in the class are not mutable

also what about the effect of using web services on client designs

Thansk if somebody can help me in order to clear my ideas

Cheers!


 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angus,

Welcome to the Ranch!

Can you be more specific about your question? Especially about the second one, I am not sure what you want with that.

What would I do if I had a class [class A] with a collection [collection C] of mutable objects (the objects can be changed) but the elements in the class are not mutable (the members of Class A are not mutable)? Such as:



If this were the case, I would not consider ClassA immutable, because its data can be changed by modifying the values in the mutable ClassB. If I wanted to make it immutable I would have to make local copies that I only have access to inside this class. I would also make sure that if I were to pass the List or any view of the List out, that I would again make copies of the mutable objects and pass out the copies.



As for your second question, I don't know the context that you mean.


** This is required to ensure the List help by ClassA instances is immutable. First, we don't know about the incoming List. Someone could change it. So we will copy from the incoming List to our own ArrayList. Then, just to make sure we can't change the List we will only hold an unmodifiable view of the new List.
 
Isaac Ferguson
Ranch Hand
Posts: 1174
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve, it has helped me a lot. You have explained it very well

Regards, Isaac
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!