Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Immutable object
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Java === A classic example of an immutable object is an instance of the Java <code>String</code> class <syntaxhighlight lang="java"> String s = "ABC"; s.toLowerCase(); // This accomplishes nothing! </syntaxhighlight> The method <code>toLowerCase()</code> does not change the data "ABC" that <code>s</code> contains. Instead, a new String object is instantiated and given the data "abc" during its construction. A reference to this String object is returned by the <code>toLowerCase()</code> method. To make the String <code>s</code> contain the data "abc", a different approach is needed: <syntaxhighlight lang="java"> s = s.toLowerCase(); </syntaxhighlight> Now the String <code>s</code> references a new String object that contains "abc". There is nothing in the syntax of the ''declaration'' of the class String that enforces it as immutable; rather, none of the String class's methods ever affect the data that a String object contains, thus making it immutable. The keyword <code>final</code> ([[Final (Java)#Final variables|detailed article]]) is used in implementing immutable primitive types and object references,<ref>{{cite web|url=http://javarevisited.blogspot.co.uk/2013/03/how-to-create-immutable-class-object-java-example-tutorial.html |title=How to create Immutable Class and Object in Java β Tutorial Example |publisher=Javarevisited.blogspot.co.uk |date=2013-03-04 |access-date=2014-04-14}}</ref> but it cannot, by itself, make ''the objects themselves'' immutable. See below examples: Primitive type variables (<code>int</code>, <code>long</code>, <code>short</code>, etc.) can be reassigned after being defined. This can be prevented by using <code>final</code>. <syntaxhighlight lang="java"> int i = 42; //int is a primitive type i = 43; // OK final int j = 42; j = 43; // does not compile. j is final so can't be reassigned </syntaxhighlight> Reference types cannot be made immutable just by using the <code>final</code> keyword. <code>final</code> only prevents reassignment. <syntaxhighlight lang="java"> final MyObject m = new MyObject(); //m is of reference type m.data = 100; // OK. We can change state of object m (m is mutable and final doesn't change this fact) m = new MyObject(); // does not compile. m is final so can't be reassigned </syntaxhighlight> Primitive wrappers (<code>Integer</code>, <code>Long</code>, <code>Short</code>, <code>Double</code>, <code>Float</code>, <code>Character</code>, <code>Byte</code>, <code>Boolean</code>) are also all immutable. Immutable classes can be implemented by following a few simple guidelines.<ref>{{cite web | url=http://www.javapractices.com/topic/TopicAction.do?Id=29 | title=Immutable objects | publisher=javapractices.com | access-date=November 15, 2012}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)