Mark As Completed Discussion

Mutable Object and Immutable Object:

An object whose states and behaviors can be changed after its creation is called a mutable object.

On the other hand, an immutable object’s states and behaviors can never be changed once it is created. The idea behind creating an immutable object is to maintain its states and behaviors throughout the course of execution of the program. It's like a shield is there which keeps you from accessing these objects.

Mutable Object And Immutable Object

Consider the following piece of Java code to understand the basic differences between mutable and immutable objects. Since our objective is to gain understanding, we will keep our example simple.

We start with a Person class, which contains certain pieces of information about a person. It has the age attribute, a constructor that returns the name, and the methods to set and get the age of a person.

1function Person(name) {
2    this.p_age = 0;
3    console.log("Name of person is :" + name);
4}
5
6Person.prototype.setAge = function(age) {
7    this.p_age = age;
8};
9
10Person.prototype.getAge = function() {
11    console.log("Age of person is :" + this.p_age);
12    return this.p_age;
13};
14
15/* Object creation */
16var myPerson = new Person("Arslan");
17
18myPerson.setAge(25);
19
20myPerson.getAge();

On executing it, we get the following output:

Mutable Object And Immutable Object

Let's say we want to make this object immutable. Sadly, we can't accomplish this by simply adding a single keyword. First, we have to create an immutable class that must include specific keywords, include certain modifiers, and must exercise restraint in the code. A simple way of enforcing this constraint would be to not provide setters for your methods to avoid any change. Let's see how to make an immutable class.

How do we create an immutable class?

In order to make a class immutable,

  • our class must be declared as final. This will restrict other classes from inheriting our class and it is one of the most secure (and simplest) ways of making sure that our class can never be extended.

  • every attribute (data member) should be declared as private and final. Making an attribute final will ensure that it can never be reassigned from within the class. On the other hand, the keyword private will ensure that the attributes will not be accessed directly from outside the class. However, it is imperative to address this part in a bit of detail.

    We can even make our class immutable by declaring the attributes as public and final. Then why declare them as private? There are various reasons. The most important one is to ensure encapsulation. Doing so will reduce the unwanted risk of cross-coupling and it will restrict us from writing code that may depend on the immutable class.

  • provide getters but never provide setters so that the object’s states cannot be changed.

The main idea behind following all of the above-mentioned practices is to restrict your users from changing the states and behaviors of the object. As a result, the instances of immutable classes are unchangeable once they are created. Therefore, immutable objects, after instantiation, become read-only forever.