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?
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 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?
- Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?
- Can my C function access data in an object of a C++ class?