Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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

 
Ranch Hand
Posts: 1272
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: 1272
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
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!