• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve, it has helped me a lot. You have explained it very well

Regards, Isaac
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic