Immutability in java
Tags: java, beginners, programming, webdev
Published: 2022-03-13 08:15:37


An immutable object is an object whose internal state remains constant after it has been entirely created. 

In other words immutable object means whose content cannot be changed. So mutable object means whose content can be changed.

This means that the public API of an immutable object guarantees us that it will behave in the same way during its whole lifetime.

Sometimes we want to create an object whose content cannot be changed after it has been created. Such an object is called immutable object and class which made that object is called immutable class.
 Ex - String objects are immutable object and String class is an 
      immutable class.


## **Immutable class**

- All data fields must be private.
- There can't be any setter methods.
- No getter fields can return a data field that is mutable.
  _If there is are getter methods all of them should return an 
   immutable object_
Class should satisfy all of these conditions to be an immutable class.

The best way to understand this is through examples. Let's see some  examples.
**Exmples 1**
```
class A1 {

    private int x;

    public A1(int x) {
        this.x = x;
    }

    public int getX() {
        return this.x;
    }
 }
```
Is this immutable or not? Check with the rules
all the data fields are private
no any setter methods
getter method doesn't return a immutable object
So this class is a immutable class.

**Example 2**
```
class A2 {

    private int x;

    public A2(int x) {
        this.x = x;
    }

    public int getX() {
        return x;
    }
    
    public void setX(int x) {
        this.x = x;
    }
}
```

Check with the rules
all the data fields are private
one setter method is there
So this class is not immutable.

I think now you have an good idea about immutable classes. let's see one more example

**Example 3**
```
class A1 {
    private int x;
    private A2 a2;

    public A1(int x, A2 a2) {
        this.x = x;
        this.a2 = a2;
    }

    public int getX() {
        return x;
    }
    
}
```
In the previous example I showed you that class A2 is not an immutable class. But keep it aside and go through the rules.

all the data fields are private
no setter methods are there
does getter method return any non immutable object? No

So this is an immutable class.

**Example 4**

```
class A1 {
    private int x;
    private A2 a2;

    public A1(int x, A2 a2) {
        this.x = x;
        this.a2 = a2;
    }

    public int getX() {
        return x;
    }

    public A2 getA2() {
        return this.a2;
    }
}
```

all the data fields are private
no setter methods are there
does getter method return any non immutable object? Yes it does.

So this is not an immutable class.

you might ask why this is not immutable. So no other way to explain that let's try to create an object of this class A1

```
 public static void main(String[] args) {
        A1 a1 = new A1(2, new A2(3));
        A2 a2 = a1.getA2(); // returning a immutable object
        a2.setX(5); // changing the value
    }
```
See x field of a2 is 3 previously. But from the line 3 we modified it to the 5. change is not a word with immutable object. Actually immutable means no change. It is the definition of immutable objects.

I think now you have a good idea about immutable object. There is keyword related with immutability in java that is final keyword. Simply final keyword won't let us change the value of a final variable. It will be a compile error. But we should remember that final only forbids us from changing the reference the variable holds, it doesn't protect us from changing the internal state of the object it refers to by using its public API.

I think this article will be too long if i talk about that in this article. 

So if you have any questions please let me know in the comment section. I'm more than happy to help.