posted 12 years ago
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.