Immutable object

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(See Also)
 
(One intermediate revision by one user not shown)
Line 7: Line 7:
 
==See Also==
 
==See Also==
  
[[Getters and setters]]
+
*[[Getters and setters]]
 +
*[[Avoid equals]]
 +
*[[Flyweight]]

Latest revision as of 23:21, 6 October 2008

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