Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable class?

How can one make a Java class immutable, what is the need of immutability and is there any advantage to using this?

like image 342
JavaUser Avatar asked Jul 02 '10 01:07

JavaUser


People also ask

What is an immutable class?

Immutable class in java means that once an object is created, we cannot change its content. In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and String class is immutable.

What is immutable class in Java with example?

Java Immutable Class In Java, when we create an object of an immutable class, we cannot change its value. For example, String is an immutable class. Hence, we cannot change the content of a string once created. Besides, we can also create our own custom immutable classes.

What is immutable example?

Summary. An object whose internal state cannot be changed is called immutable for example a number, a string, and a tuple. An object whose internal state can be changed is called mutable for example a list, a set, and a dictionary.

What are all immutable classes?

Immutable class means once the object of the class is created its fields cannot be modified or changed. In Java, all the wrapper classes like Boolean, Short, Integer, Long, Float, Double, Byte, Char, and String classes are immutable classes.


1 Answers

What is an immutable object?

An immutable object is one that will not change state after it is instantiated.

How to make an object immutable?

In general, an immutable object can be made by defining a class which does not have any of its members exposed, and does not have any setters.

The following class will create an immutable object:

class ImmutableInt {   private final int value;    public ImmutableInt(int i) {     value = i;   }    public int getValue() {     return value;   } } 

As can be seen in the above example, the value of the ImmutableInt can only be set when the object is instantiated, and by having only a getter (getValue) the object's state cannot be changed after instantiation.

However, there must be care taken that all objects that are referenced by the object must be immutable as well, or it could be possible to change the state of the object.

For example, allowing an reference to an array or ArrayList to be obtained through an getter will allow the internal state to change by changing the array or collection:

class NotQuiteImmutableList<T> {   private final List<T> list;    public NotQuiteImmutableList(List<T> list) {     // creates a new ArrayList and keeps a reference to it.     this.list = new ArrayList(list);    }    public List<T> getList() {     return list;   } } 

The problem with the above code is, that the ArrayList can be obtained through getList and be manipulated, leading to the state of the object itself to be altered, therefore, not immutable.

// notQuiteImmutableList contains "a", "b", "c" List<String> notQuiteImmutableList= new NotQuiteImmutableList(Arrays.asList("a", "b", "c"));  // now the list contains "a", "b", "c", "d" -- this list is mutable. notQuiteImmutableList.getList().add("d"); 

One way to get around this problem is to return a copy of an array or collection when called from a getter:

public List<T> getList() {   // return a copy of the list so the internal state cannot be altered   return new ArrayList(list); } 

What is the advantage of immutability?

The advantage of immutability comes with concurrency. It is difficult to maintain correctness in mutable objects, as multiple threads could be trying to change the state of the same object, leading to some threads seeing a different state of the same object, depending on the timing of the reads and writes to the said object.

By having an immutable object, one can ensure that all threads that are looking at the object will be seeing the same state, as the state of an immutable object will not change.

like image 110
coobird Avatar answered Sep 18 '22 16:09

coobird