An immutable class is simply a class whose instances cannot be modified. All of the information contained in each instance is provided when it is created and is fixed for the lifetime of the object.
JDK immutable classes: String, the boxed primitive classes(wrapper classes), BigInteger and BigDecimal etc.
How to make a class immutable?
1. Don’t provide any methods that modify the object’s state (known as mutators).
2. Ensure that the class can’t be extended.
3. Make all fields final.
4. Make all fields private.
This prevents clients from obtaining access to mutable objects referred to by fields and modifying these objects directly.
5. Make defensive copies.
Ensure exclusive access to any mutable components.
public List getList() {
return Collections.unmodifiableList(list); <=== defensive copy of the mutable
field before returning it to the caller
}
If your class has any fields that refer to mutable objects, ensure that clients of the class cannot obtain references to these objects. Never initialize such a field to a client-provided object reference or return the object reference from an accessor.
import java.util.Date;
public final class ImmutableClass {
public ImmutableClass(int id, String name, Date doj) {
this.id = id;
this.name = name;
this.doj = new Date(doj.getTime());
}
private final int id;
private final String name;
private final Date doj;
public int getId() {
return id;
}
public String getName() {
return name;
}
/**
* Date class is mutable so we need a little care here.
* We should not return the reference of original instance variable.
* Instead of a new Date object, with content copied to it, should be returned.
* */
public Date getDoj() {
return new Date(doj.getTime()); // For mutable fields
}
}
import java.util.Date;
public class TestImmutable {
public static void main(String[] args) {
String name = "raj";
int id = 1;
Date doj = new Date();
ImmutableClass class1 = new ImmutableClass(id, name, doj);
ImmutableClass class2 = new ImmutableClass(id, name, doj);
// every time will get a new reference for same object. Modification in reference will not affect the immutability because it is temporary reference.
Date date = class1.getDoj();
date.setTime(date.getTime()+122435);
System.out.println(class1.getDoj()==class2.getDoj());
}
}
Ensure that the class can’t be extended?
public class Immutable {
private final int value;
public Immutable(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Now, suppose I do the following:
public class Mutable extends Immutable {
private int realValue;
public Mutable(int value) {
super(value);
realValue = value;
}
public int getValue() {
return realValue;
}
public void setValue(int newValue) {
realValue = newValue;
}
}
Why Immutability?
1. They are less prone to error and are more secure.
2. Immutable classes are easier to design, implement, and use than mutable classes.
3. Immutable objects are thread-safe so there are no synchronization issues.
4. Immutable objects are good Map keys and Set elements since these typically do not change once created.
5. Immutability makes it easier to write, use and reason about the code (class invariant is established once and then unchanged).
6. Immutability makes it easier to parallelize program as there are no conflicts among objects.
7. The internal state of the program will be consistent even if you have exceptions.
8. References to immutable objects can be cached as they are not going to change.(i.e in Hashing it provide fast operations).
Hi,
ReplyDeleteThe above mentioned class is not immutable,
suppose if we modify the doj object then date object in class1 and class2 also gets modify,
So care needs to be taken care in constructor when we set the mutable object.
public final class ImmutableClass {
public ImmutableClass(int id, String name, Date doj) {
this.id = id;
this.name = name;
this.doj = new Date(doj.getTime());
}
..............
Thanks for inputs Mr. Koti.
Deleteclass Immutable {
ReplyDeleteprivate final int value;
public Immutable(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
public class MutableClass extends Immutable {
private int realValue;
public MutableClass(int value){
super(value);
realValue=value;
}
public static void main(String[] args) {
Immutable immutable = new Immutable(5);
MutableClass mutable= new MutableClass(10);
}
}
Even if we're not making Immutable class Final , how come above code is changing immutable object value which is 5.
When MutableClass constructor is being called, it is not affecting Immutable object value.
Correct me if i'm wrong
Hello Mr. Paras,
DeletePlease look into the below given example.
http://javaexplorer03.blogspot.in/2017/06/why-would-one-declare-immutable-class.html
Regards
https://bayanlarsitesi.com/
ReplyDeleteKayseri
Sinop
Kilis
Hakkari
İGGF5A
Malatya
ReplyDeleteKırıkkale
Aksaray
Bitlis
Manisa
6Bİ1HU
Antep Lojistik
ReplyDeleteYalova Lojistik
Erzincan Lojistik
Tekirdağ Lojistik
Elazığ Lojistik
CTTSGR
222E1
ReplyDeleteGiresun Evden Eve Nakliyat
Kırşehir Evden Eve Nakliyat
Gümüşhane Evden Eve Nakliyat
Kırşehir Lojistik
Bolu Evden Eve Nakliyat
06CAC
ReplyDeleteErzincan Parça Eşya Taşıma
Sinop Şehir İçi Nakliyat
Alya Coin Hangi Borsada
İzmir Şehirler Arası Nakliyat
Bone Coin Hangi Borsada
Adana Şehir İçi Nakliyat
Niğde Şehirler Arası Nakliyat
Tunceli Şehir İçi Nakliyat
Karabük Evden Eve Nakliyat
F8DC3
ReplyDeleteAntalya Telefonda Canlı Sohbet
samsun canli sohbet bedava
gümüşhane rastgele görüntülü sohbet
adana en iyi görüntülü sohbet uygulamaları
Izmir Seslı Sohbet Sıtelerı
bayburt mobil sohbet chat
edirne rastgele sohbet odaları
afyon yabancı sohbet
kilis yabancı görüntülü sohbet uygulamaları
1A065
ReplyDeleteBolu Kadınlarla Sohbet Et
zonguldak kadınlarla sohbet
sivas telefonda sohbet
bolu seslı sohbet sıtelerı
Mersin Ücretsiz Sohbet
Hakkari Rastgele Sohbet Siteleri
malatya canlı sohbet et
Hatay Kadınlarla Rastgele Sohbet
gümüşhane canlı sohbet uygulamaları
FE244
ReplyDeletekaraman parasız sohbet
nevşehir sohbet sitesi
Batman Parasız Görüntülü Sohbet Uygulamaları
balıkesir ücretsiz sohbet uygulamaları
kayseri yabancı canlı sohbet
istanbul sesli sohbet sitesi
sohbet chat
sivas canlı görüntülü sohbet
niğde tamamen ücretsiz sohbet siteleri
45A96
ReplyDeletehakkari rastgele görüntülü sohbet uygulamaları
isparta en iyi sesli sohbet uygulamaları
ücretsiz görüntülü sohbet
görüntülü sohbet
tekirdağ mobil sohbet chat
amasya kadınlarla sohbet
bingöl görüntülü sohbet uygulama
kocaeli bedava görüntülü sohbet
Çankırı Bedava Sohbet Uygulamaları
CEDB1
ReplyDeleteKwai Takipçi Satın Al
Binance Kaldıraçlı İşlem Nasıl Yapılır
Apenft Coin Hangi Borsada
Twitter Retweet Satın Al
Binance Madencilik Nasıl Yapılır
Youtube Beğeni Hilesi
Soundcloud Reposts Hilesi
Coin Çıkarma
Coin Para Kazanma
TGHBTGHYTGHJ
ReplyDeleteشركة كشف تسربات المياه بالاحساء
شركة تنظيف سجاد بالجبيل RKgILLaDtS
ReplyDeleteشركة مكافحة حشرات 449wkaVnRO
ReplyDeleteشركة رش حشرات بالاحساء YLBBvi3Ny9
ReplyDeleteشركة مكافحة الحمام بالجبيل EVyijVgJKU
ReplyDelete7EEAA0DDD2
ReplyDeletegalxe
puffer
aethir stake
dymension
rocketpool
medi finance
tokenfi
moonbeam
puffer finance