{"id":29735,"date":"2014-09-05T10:00:35","date_gmt":"2014-09-05T07:00:35","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=29735"},"modified":"2014-09-05T08:14:06","modified_gmt":"2014-09-05T05:14:06","slug":"objects-should-be-immutable","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html","title":{"rendered":"Objects Should Be Immutable"},"content":{"rendered":"<p>In object-oriented programming, an object is <a href=\"http:\/\/en.wikipedia.org\/wiki\/Immutable_object\">immutable<\/a> if its state can&#8217;t be modified after it is created.<\/p>\n<p>In Java, a good example of an immutable object is <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/String.html\"><code>String<\/code><\/a>. Once created, we can&#8217;t modify its state. We can request that it creates new strings, but its own state will never change.<\/p>\n<p>However, there are not so many immutable classes in JDK. Take, for example, class <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Date.html\"><code>Date<\/code><\/a>. It is possible to modify its state using <code>setTime()<\/code>.<\/p>\n<p>I don&#8217;t know why the JDK designers decided to make these two very similar classes differently. However, I believe that the design of a mutable <code>Date<\/code> has a many flaws, while the immutable <code>String<\/code> is much more in the spirit of the object-oriented paradigm.<\/p>\n<p>Moreover, I think that <strong>all classes should be immutable in a perfect object-oriented world<\/strong>. Unfortunately, sometimes, it is technically not possible due to limitations in JVM. Nevertheless, we should always aim for the best.<\/p>\n<p>This is an incomplete list of arguments in favor of immutability:<\/p>\n<ul>\n<li>immutable objects are simpler to construct, test, and use<\/li>\n<li>truly immutable objects are always thread-safe<\/li>\n<li>they help to avoid temporal coupling<\/li>\n<li>their usage is side-effect free (no defensive copies)<\/li>\n<li>identity mutability problem is avoided<\/li>\n<li>they always have failure atomicity<\/li>\n<li>they are much easier to cache<\/li>\n<li>they prevent NULL references, <a href=\"\/2014\/05\/13\/why-null-is-bad.html\">which are bad<\/a><\/li>\n<\/ul>\n<p>Let&#8217;s discuss the most important arguments one by one.<\/p>\n<h2>Thread Safety<\/h2>\n<p>The first and the most obvious argument is that immutable objects are thread-safe. This means that multiple threads can access the same object at the same time, without clashing with another thread.<\/p>\n<p>If no object methods can modify its state, no matter how many of them and how often are being called parallel \u2014 they will work in their own memory space in stack.<\/p>\n<p>Goetz et al. explained the advantages of immutable objects in more details in their very famous book <a href=\"http:\/\/www.amazon.com\/gp\/product\/0321349601\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321349601&amp;linkCode=as2&amp;tag=yegor256com-20&amp;linkId=OHVW5XBUDRTKVT46\">Java Concurrency in Practice<\/a> (highly recommended).<\/p>\n<h2>Avoiding Temporal Coupling<\/h2>\n<p>Here is an example of temporal coupling (the code makes two consecutive HTTP POST requests, where the second one contains HTTP body):<\/p>\n<pre class=\" brush:java\">Request request = new Request(\"http:\/\/example.com\");\r\nrequest.method(\"POST\");\r\nString first = request.fetch();\r\nrequest.body(\"text=hello\");\r\nString second = request.fetch();<\/pre>\n<p>This code works. However, you must remember that the first request should be configured before the second one may happen. If we decide to remove the first request from the script, we will remove the second and the third line, and won&#8217;t get any errors from the compiler:<\/p>\n<pre class=\" brush:java\">Request request = new Request(\"http:\/\/example.com\");\r\n\/\/ request.method(\"POST\");\r\n\/\/ String first = request.fetch();\r\nrequest.body(\"text=hello\");\r\nString second = request.fetch();<\/pre>\n<p>Now, the script is broken although it compiled without errors. This is what temporal coupling is about \u2014 there is always some hidden information in the code that a programmer has to remember. In this example, we have to remember that the configuration for the first request is also used for the second one.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>We have to remember that the second request should always stay together and be executed after the first one.<\/p>\n<p>If <code>Request<\/code> class were immutable, the first snippet wouldn&#8217;t work in the first place, and would have been rewritten like:<\/p>\n<pre class=\" brush:java\">final Request request = new Request(\"\");\r\nString first = request.method(\"POST\").fetch();\r\nString second = request.method(\"POST\").body(\"text=hello\").fetch();<\/pre>\n<p>Now, these two requests are not coupled. We can safely remove the first one, and the second one will still work correctly. You may point out that there is a code duplication. Yes, we should get rid of it and re-write the code:<\/p>\n<pre class=\" brush:java\">final Request request = new Request(\"\");\r\nfinal Request post = request.method(\"POST\");\r\nString first = post.fetch();\r\nString second = post.body(\"text=hello\").fetch();<\/pre>\n<p>See, refactoring didn&#8217;t break anything and we still don&#8217;t have temporal coupling. The first request can be removed safely from the code without affecting the second one.<\/p>\n<p>I hope this example demonstrates that the code manipulating immutable objects is more readable and maintainable, b ecause it doesn&#8217;t have temporal coupling.<\/p>\n<h2>Avoiding Side Effects<\/h2>\n<p>Let&#8217;s try to use our <code>Request<\/code> class in a new method (now it is mutable):<\/p>\n<pre class=\" brush:java\">public String post(Request request) {\r\n  request.method(\"POST\");\r\n  return request.fetch();\r\n}<\/pre>\n<p>Let&#8217;s try to make two requests \u2014 the first with GET method and the second with POST:<\/p>\n<pre class=\" brush:java\">Request request = new Request(\"http:\/\/example.com\");\r\nrequest.method(\"GET\");\r\nString first = this.post(request);\r\nString second = request.fetch();<\/pre>\n<p>Method <code>post()<\/code> has a &#8220;side effect&#8221; \u2014 it makes changes to the mutable object <code>request<\/code>. These changes are not really expected in this case. We expect it to make a POST request and return its body. We don&#8217;t want to read its documentation just to find out that behind the scene it also modifies the request we&#8217;re passing to it as an argument.<\/p>\n<p>Needless to say, such side effects lead to bugs and maintainability issues. It would be much better to work with an immutable <code>Request<\/code>:<\/p>\n<pre class=\" brush:java\">public String post(Request request) {\r\n  return request.method(\"POST\").fetch();\r\n}<\/pre>\n<p>In this case, we may not have any side effects. Nobody can modify our <code>request<\/code> object, no matter where it is used and how deep through the call stack it is passed by method calls:<\/p>\n<pre class=\" brush:java\">Request request = new Request(\"http:\/\/example.com\").method(\"GET\");\r\nString first = this.post(request);\r\nString second = request.fetch();<\/pre>\n<p>This code is perfectly safe and side effect free.<\/p>\n<h2>Avoiding Identity Mutability<\/h2>\n<p>Very often, we want objects to be identical if their internal states are the same. <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Date.html\"><code>Date<\/code><\/a> class is a good example:<\/p>\n<pre class=\" brush:java\">Date first = new Date(1L);\r\nDate second = new Date(1L);\r\nassert first.equals(second); \/\/ true<\/pre>\n<p>There are two different objects; however, they are equal to each other because their encapsulated states are the same. This is made possible through their custom overloaded implementation of <code>equals()<\/code> and <code>hashCode()<\/code> methods.<\/p>\n<p>The consequence of this convenient approach being used with mutable objects is that every time we modify object&#8217;s state it changes its identity:<\/p>\n<pre class=\" brush:java\">Date first = new Date(1L);\r\nDate second = new Date(1L);\r\nfirst.setTime(2L);\r\nassert first.equals(second); \/\/ false<\/pre>\n<p>This may look natural, until you start using your mutable objects as keys in maps:<\/p>\n<pre class=\" brush:java\">Map&lt;Date, String&gt; map = new HashMap&lt;&gt;();\r\nDate date = new Date();\r\nmap.put(date, \"hello, world!\");\r\ndate.setTime(12345L);\r\nassert map.containsKey(date); \/\/ false<\/pre>\n<p>When modifying the state of <code>date<\/code> object, we&#8217;re not expecting it to change its identity. We&#8217;re not expecting to lose an entry in the map just because the state of its key is changed. However, this is exactly what is happening in the example above.<\/p>\n<p>When we add an object to the map, its <code>hashCode()<\/code> returns one value. This value is used by <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/HashMap.html\"><code>HashMap<\/code><\/a> to place the entry into the internal hash table. When we call <code>containsKey()<\/code> hash code of the object is different (because it is based on its internal state) and <code>HashMap<\/code> can&#8217;t find it in the internal hash table.<\/p>\n<p>It is a very annoying and difficult to debug side effects of mutable objects. Immutable objects avoid it completely.<\/p>\n<h2>Failure Atomicity<\/h2>\n<p>Here is a simple example:<\/p>\n<pre class=\" brush:java\">public class Stack {\r\n  private int size;\r\n  private String[] items;\r\n  public void push(String item) {\r\n    size++;\r\n    if (size &gt; items.length) {\r\n      throw new RuntimeException(\"stack overflow\");\r\n    }\r\n    items[size] = item;\r\n  }\r\n}<\/pre>\n<p>It is obvious that an object of class <code>Stack<\/code> will be left in a broken state if it throws a runtime exception on overflow. Its <code>size<\/code> property will be incremented, while <code>items<\/code> won&#8217;t get a new element.<\/p>\n<p>Immutability prevents this problem. An object will never be left in a broken state because its state is modified only in its constructor. The constructor will either fail, rejecting object instantiation, or succeed, making a valid solid object, which never changes its encapsulated state.<\/p>\n<p>For more on this subject, read <a href=\"http:\/\/www.amazon.com\/gp\/product\/0321356683\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321356683&amp;linkCode=as2&amp;tag=yegor256com-20&amp;linkId=CSSI3POG6ZJ3BJ6T\">Effective Java, 2nd Edition<\/a> by Joshua Bloch.<\/p>\n<h2>Arguments Against Immutability<\/h2>\n<p>There are a number of arguments against immutability.<\/p>\n<ol>\n<li>\u201cImmutability is not for enterprise systems\u201d. Very often, I hear people say that immutability is a fancy feature, while absolutely impractical in real enterprise systems. As a counter-argument, I can only show some examples of real-life applications that contain only immutable Java objects: <a href=\"http:\/\/http.jcabi.com\">jcabi-http<\/a>, <a href=\"http:\/\/xml.jcabi.com\">jcabi-xml<\/a>, <a href=\"http:\/\/github.jcabi.com\">jcabi-github<\/a>, <a href=\"http:\/\/s3.jcabi.com\">jcabi-s3<\/a>, <a href=\"http:\/\/dynamo.jcabi.com\">jcabi-dynamo<\/a>, <a href=\"http:\/\/simpledb.jcabi.com\">jcabi-simpledb<\/a> The above are all Java libraries that work solely with immutable classes\/objects. <a href=\"https:\/\/github.com\/netbout\/netbout\">netbout.com<\/a> and <a href=\"https:\/\/github.com\/sttc\/stateful\">stateful.co<\/a> are web applications that work solely with immutable objects.<\/li>\n<li>\u201cIt&#8217;s cheaper to update an existing object than create a new one\u201d. Oracle <a href=\"http:\/\/docs.oracle.com\/javase\/tutorial\/essential\/concurrency\/immutable.html\">thinks<\/a> that \u201cThe impact of object creation is often overestimated and can be offset by some of the efficiencies associated with immutable objects. These include decreased overhead due to garbage collection, and the elimination of code needed to protect mutable objects from corruption.\u201d I agree.<\/li>\n<\/ol>\n<p>If you have some other arguments, please post them below and I&#8217;ll try to comment.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/www.yegor256.com\/2014\/06\/09\/objects-should-be-immutable.html\">Objects Should Be Immutable<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\">JCG partner<\/a> Yegor Bugayenko at the <a href=\"http:\/\/www.yegor256.com\/\">About Programming<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In object-oriented programming, an object is immutable if its state can&#8217;t be modified after it is created. In Java, a good example of an immutable object is String. Once created, we can&#8217;t modify its state. We can request that it creates new strings, but its own state will never change. However, there are not so &hellip;<\/p>\n","protected":false},"author":593,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-29735","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Objects Should Be Immutable<\/title>\n<meta name=\"description\" content=\"In object-oriented programming, an object is immutable if its state can&#039;t be modified after it is created. In Java, a good example of an immutable object\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Objects Should Be Immutable\" \/>\n<meta property=\"og:description\" content=\"In object-oriented programming, an object is immutable if its state can&#039;t be modified after it is created. In Java, a good example of an immutable object\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/yegor256\" \/>\n<meta property=\"article:published_time\" content=\"2014-09-05T07:00:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yegor Bugayenko\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/yegor256\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yegor Bugayenko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html\"},\"author\":{\"name\":\"Yegor Bugayenko\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/f3ff9c97ecd948f271ebc5ead401d02d\"},\"headline\":\"Objects Should Be Immutable\",\"datePublished\":\"2014-09-05T07:00:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html\"},\"wordCount\":1228,\"commentCount\":9,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html\",\"name\":\"Objects Should Be Immutable\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2014-09-05T07:00:35+00:00\",\"description\":\"In object-oriented programming, an object is immutable if its state can't be modified after it is created. In Java, a good example of an immutable object\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/09\\\/objects-should-be-immutable.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Objects Should Be Immutable\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/f3ff9c97ecd948f271ebc5ead401d02d\",\"name\":\"Yegor Bugayenko\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"caption\":\"Yegor Bugayenko\"},\"description\":\"Yegor Bugayenko is an Oracle certified Java architect, CEO of Zerocracy, author of Elegant Objects book series about object-oriented programing, lead architect and founder of Cactoos, Takes, Rultor and Jcabi, and a big fan of test automation.\",\"sameAs\":[\"http:\\\/\\\/www.yegor256.com\\\/\",\"https:\\\/\\\/www.facebook.com\\\/yegor256\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/yegor256\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/yegor256\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/yegor-bugayenko\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Objects Should Be Immutable","description":"In object-oriented programming, an object is immutable if its state can't be modified after it is created. In Java, a good example of an immutable object","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html","og_locale":"en_US","og_type":"article","og_title":"Objects Should Be Immutable","og_description":"In object-oriented programming, an object is immutable if its state can't be modified after it is created. In Java, a good example of an immutable object","og_url":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/yegor256","article_published_time":"2014-09-05T07:00:35+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Yegor Bugayenko","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/yegor256","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yegor Bugayenko","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html"},"author":{"name":"Yegor Bugayenko","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/f3ff9c97ecd948f271ebc5ead401d02d"},"headline":"Objects Should Be Immutable","datePublished":"2014-09-05T07:00:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html"},"wordCount":1228,"commentCount":9,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html","url":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html","name":"Objects Should Be Immutable","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2014-09-05T07:00:35+00:00","description":"In object-oriented programming, an object is immutable if its state can't be modified after it is created. In Java, a good example of an immutable object","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Objects Should Be Immutable"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/f3ff9c97ecd948f271ebc5ead401d02d","name":"Yegor Bugayenko","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","caption":"Yegor Bugayenko"},"description":"Yegor Bugayenko is an Oracle certified Java architect, CEO of Zerocracy, author of Elegant Objects book series about object-oriented programing, lead architect and founder of Cactoos, Takes, Rultor and Jcabi, and a big fan of test automation.","sameAs":["http:\/\/www.yegor256.com\/","https:\/\/www.facebook.com\/yegor256","https:\/\/www.linkedin.com\/in\/yegor256","https:\/\/x.com\/https:\/\/twitter.com\/yegor256"],"url":"https:\/\/www.javacodegeeks.com\/author\/yegor-bugayenko"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/29735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/593"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=29735"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/29735\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=29735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=29735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=29735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}