Search code examples
javamutability

Is an object that contains a fixed set of mutable objects mutable?


Mutability still confuses me from time to time.

If I have an object that contains a fixed set of mutable objects. Do you consider this object mutable?

Example: I have an object called Puzzle which containts a fixed set of Pieces. The Pieces are mutable. For example, they can be upsideDown and its orientation can change. Assuming you cannot loose pieces in this example (if only that were true in real life...), I guess the Puzzle object is still immutable, right?


Solution

  • From wikipedia: In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created. This is in contrast to a mutable object (changeable object), which can be modified after it is created.

    In your example the Puzzle object is mutable because you can change the state of one of its Pieces.