<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">Immutable class is a class which once created, it’s contents can not be changed. Immutable objects are the objects whose state can not be changed once constructed. e.g. String class</p>
<p>An immutable class is one whose state can not be changed once created. There are certain guidelines to create an class immutable.</p>
<p><b>Guidelines to make a class immutable-</b></p>
<ol style="text-align: left;">
<li>Create a final class.</li>
<li>Set the values of properties using constructor only.</li>
<li>Make the properties of the class final and private</li>
<li>Do not provide any setters for these properties.</li>
<li>If the instance fields include references to mutable objects, don&#8217;t allow those objects to be changed:
<ol style="text-align: left;">
<li>Don&#8217;t provide methods that modify the mutable objects.</li>
<li>Don&#8217;t share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.</li>
</ol>
</li>
</ol>
<div align='center' id="ads-id"></div>
<pre class="highlight" name="code">public final class FinalPersonClass {

 private final String name; 
 private final int age; 
 
 public FinalPersonClass(final String name, final int age) { 
 super(); 
 this.name = name; 
 this.age = age; 
 } 
 public int getAge() { 
 return age; 
 } 
 public String getName() { 
 return name; 
 } 
 
}
</pre>
<p><b>All wrapper classes in java.lang are immutable –</b> <br />
String, Integer, Boolean, Character, Byte, Short, Long, Float, Double, BigDecimal, BigInteger</p>
<p><b>What are the advantages of immutability?</b><br />
The advantages are: <br />
1) Immutable objects are automatically thread-safe, the overhead caused due to use of synchronisation is avoided.<br />
2) Once created the state of the immutable object can not be changed so there is no possibility of them getting into an inconsistent state.<br />
3) The references to the immutable objects can be easily shared or cached without having to copy or clone them as there state can not be changed ever after construction.<br />
4) The best use of the immutable objects is as the keys of a map.</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/network-as-service-naas/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/object-cloning-in-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '192'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…