{"id":2700,"date":"2012-02-03T18:04:46","date_gmt":"2012-02-03T12:34:46","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2700"},"modified":"2012-02-03T18:04:46","modified_gmt":"2012-02-03T12:34:46","slug":"how-to-create-immutable-class-in-java","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/how-to-create-immutable-class-in-java\/","title":{"rendered":"How to create Immutable Class in Java"},"content":{"rendered":"<p style=\"text-align: justify;\">We have heard the word &#8220;<strong>Immutable class<\/strong>&#8221; lots of time in Java. The best example is class &#8220;<strong><a title=\"Immutable nature of string\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/immutable-nature-of-string-java\/\" target=\"_blank\">String<\/a><\/strong>&#8220;. Immutable class is the class whose value cannot be changed throughout the life cycle. We cannot change the content of String class, everytime new reference is created when we change the content, that is the basic difference between <strong>String<\/strong> and <strong>StringBuffer<\/strong> Class.<br \/>\nIn this article, i will explain the step by step process to create the custom Immutable class in Java.<br \/>\nOur class should not able to derived and for that we will declare our class as final. The values cannot be change and thats why we will declare all the variables as final and we will provide only the getter methods as we cannot write setters because of final variables.<br \/>\nSo to summarize, following steps needs to be taken:<!--more--><\/p>\n<ol>\n<li><strong>declare class as final<\/strong><\/li>\n<li><strong>declare all variables as final<\/strong><\/li>\n<li><strong>provide constructor to set the values<\/strong><\/li>\n<li><strong>provide getter<\/strong><\/li>\n<\/ol>\n<p>Source code:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage in.shivasoft.demo;\n\nfinal class Immutable\n{\n\tprivate final int val1;\n\tprivate final String val2;\n\n\tpublic Immutable(int a, String s)\n\t{\n\t\tval1 = a;\n\t\tval2 = s;\n\t}\n\tpublic int getVal1()\n\t{\n\t\treturn val1;\n\t}\n\tpublic String getVal2()\n\t{\n\t\treturn val2;\n\t}\n}\n\npublic class ImmutableClassDemo {\n\tpublic static void main(String&#x5B;] args) {\n\t\tImmutable obj = new Immutable(10, &quot;ShivaSoft ...the supreme solution&quot;);\n\t\tSystem.out.println(obj.getVal1());\n\t\tSystem.out.println(obj.getVal2());\n\t}\n}\n<\/pre>\n<p>Output :<\/p>\n<blockquote><p>10<br \/>\nShivaSoft &#8230;the supreme solution<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>creating the Immutable class in Java with simple source code example<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[101,329],"class_list":["post-2700","post","type-post","status-publish","format-standard","hentry","category-java","tag-final","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":536,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/immutable-nature-of-string-java\/","url_meta":{"origin":2700,"position":0},"title":"Immutable nature of String &#8211; JAVA","author":"Jitendra","date":"July 5, 2010","format":false,"excerpt":"Immutable nature of String - JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Create instance of string in JAVA","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/String-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2061,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/explain-serialversionuid-in-java\/","url_meta":{"origin":2700,"position":1},"title":"Explain serialVersionUID in Java","author":"Jitendra","date":"April 20, 2011","format":false,"excerpt":"What is serialVersionUID and what is its need ?","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5434,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/creating-custom-ant-plugin-or-task-in-java\/","url_meta":{"origin":2700,"position":2},"title":"Creating custom ANT plugin or Task in Java","author":"Jitendra","date":"March 19, 2016","format":false,"excerpt":"Tutorial to create custom Task in ANT with complete source code","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Create custom ANT Task in Java","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":2017,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-j2ee-interview-questions-1\/","url_meta":{"origin":2700,"position":3},"title":"Java &#8211; J2EE Interview Questions &#8211; 1","author":"Jitendra","date":"April 15, 2011","format":false,"excerpt":"JAVA - J2EE Interview Questions - 1, JAVA - J2EE Interview Questions - 1,custom tag JSP, Externalization , serialVersionUID, difference between interface and abstract class, iterate HashMap","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1612,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/serialization-marshalling-deflating-in-java\/","url_meta":{"origin":2700,"position":4},"title":"Serialization \/ Marshalling \/ Deflating in JAVA","author":"Jitendra","date":"March 9, 2011","format":false,"excerpt":"Concept of Serialization \/ Marshalling \/ Deflating in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Serialization ,Marshalling ,deflating, DeSerialization ,UnMarshalling ,inflating","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Serialization-Marshalling-deflating-DeSerialization-UnMarshalling-inflating.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":700,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/interface\/","url_meta":{"origin":2700,"position":5},"title":"Interface in JAVA","author":"Jitendra","date":"August 2, 2010","format":false,"excerpt":"Explains the usage and benefits of interface in JAVA.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2700"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2700\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}