Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What if the constructor take a mutable object reference as parameter, or a data member is a reference to a object, is this class still mutable?

0
Posted

What if the constructor take a mutable object reference as parameter, or a data member is a reference to a object, is this class still mutable?

0

Constructor seems opening a hole here, if we only copy a reference of an object as a data member (attribute), since the object being referred might change. However, what is the definition of immutable class? “A String object is immutable, that is, its contents never change, while an array of char has mutable elements. ” —-quotation from JLS 10.9 I think as long as the data member (contents of the class) cannot be changed, the class is immutable. If the data member refers to something else, the change of something else does not necessary mean the class is mutable, since they are NOT contents of the class, only their references are. This is similar to the difference between aggregation and composition in UML (or OO). The object is destroyed; the composition parts disappear too. The aggregation can be still there. The immutable object can be GC-ed, but the something-else which its data member referring to can be still there. This might not be something in the mind of Java creators. Who

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123