In computer science, an immutable object, as opposed to a mutable object, is a kind of object whose internal states cannot be modified in object oriented programming (OOP).

In some languages, objects are handled as a reference rather than a concrete value. Many OOP langauges take that scheme, including Java programming language and Ruby programming language. In that case, it matters whether the state of object can vary or not.

Handling mutable objects is usually more cumbersome than immutable ones. To avoid surprising changes, you need to use a defensive copy. This trick often slows down runtime performance.

It is recommended that immutable data should always be passed to the constructor. It should not be set via an accessor. See accumulate and fire for a description of this problem.

Strings or other concrete objects are typically expressed as immutable object to improve readibility and runtime efficiency in object-oriented programming. In Python and Java, strings are immutable objects while Java also has a class StringBuffer as mutable object.

The aritcle contains materials partly from Perl Design Patterns Book.

External Links