This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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

 
Ranch Hand
Posts: 1325
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: 4179
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.
 
Angus Ferguson
Ranch Hand
Posts: 1325
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
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!