Immutable object

From CSSEMediaWiki
Jump to: navigation, search

An immutable object is one in which the value or state can not be changed.

The value is set when the object is created. If the value of the object needs to be changed then the object is replaced by an object containing the new value. The string object in Java is an example of an immutable object. Any string operations in Java return a new object containing the result of the operation. Using immutable objects removes synchronisation problems that can occur in multi threaded programmes. It also removes worries on some level about side effects.

Many concepts will not work without the concept of immutable objects such as flyweight and the "canonical representation" mechanism for strings in Java.

See Also

Personal tools