What is shared mutability in Java

Hashan Mahesh
5 min readJun 3, 2020

The terms Mutability and Immutability in programming are very important topics to understand the behavior of a language. It’s always better if you have a great foundation of knowledge in those core concepts of programming. In this little article, what my intention is to give an insight of the term shared mutability. But before learning about that, you indeed need an understanding of Mutability and Immutability itself.

So, let’s briefly look at those two things.

What is the main difference between Mutability & Immutability

The main difference here is that the immutable objects are unchangeable after it is created.

Let’s see some examples in Java

public class Car {
private String color;

public Car(String color) {
this.color = color;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}
}
public class Mutability {
public static void main(String[] args) {

//creating the car object
Car car = new Car("Red");
System.out.println(car.getColor());// it prints Red
//The color of that car object is set to white
car.setColor("White");
System.out.println(car.getColor());// it prints White
}
}

According to the definition of Immutability, an object can not be changed after it is created, but if you look at the example above, the color of the car is changed to white from red. which means the above program is an example for Mutability.

The most simplest example for an Immutable object in Java is the String object. String is Immutable by default in Java, and if you want to make it mutable you have to use the Wrapper classes called StringBuilder or StringBuffer. There is a big difference between these two class called Thread safety which you will get to know in this article, but if you want more details about these Wrapper classes just visit here-

Now let’s see some examples which proves that the Strings are immutable.

public class Immutability {
public static void main(String[] args) {
String name = "Hashan";
String first_name = name;

System.out.println(
"name "+name+" first_name "+first_name);

first_name = "mahesh";

System.out.println(
"name "+name+" first_name "+first_name);
}
}

What’s happening here is if you run this program, it will print,

name Hashan first_name Hashan
name Hashan first_name mahesh

What it proves us is that even-though you change the value of the “first_name” variable that aliases to the “name” variable, the value for the “name” variable does not change. Why this happens is because of the Immutability. Actually what happens in Java string is that it creates new object instances as many as you change the value of a string variable, which means when you change the value of “first_name” variable, it creates a new object and assign the value as “mahesh” to that object. Thus, the original value of the “name” variable still remains unchanged. This is what you called Immutability.

I think that is enough for Mutability and Immutability, if you want more information just google it and come here :).

Okay now let’s move on to the topic today

SHARED MUTABILITY

There is a famous saying, said by Venkat Subramaniam, who is an instructional professor at the University of Houston.

“Mutability is OK, sharing is nice, shared mutability is devil’s work.”:

Let’s understand this

As you know Mutability in Programming is nothing but the ability to change the state of an object after it is created. So, is it bad to change an object after it is created? No right. That’s why he said “MUTABILITY IS OK”

Second point:- is it bad to share something with others. For instance,is it bad to share some library files which you created, with others? No right. So, Yes “SHARING IS NICE”

here comes the part, “SHARED MUTABILITY IS DEVIL’S WORK”. What does that mean?

Simply Shared Mutability means sharing the ability of changing an object’s state after it is created, with others. Why it is BAD?

Let’s understand this with an example

Consider the following two classes,

As you see here, the Sample class has only one class variable which is “number”, and two methods. The setNumber() method increments the value of “number” by 10 and the getNumber() will return the current value of the “number” variable, as you call them.

The setNumber() method will stop the execution for 5ms of the current thread for each iteration of the for loop.

Now let’s create the main method,

In main method, the setNumber() method which is defined in the Sample class will be called in parallel by 100 different threads.

NOTE- If you want to learn more about Java threads and thread pools visit here

Here, what happens is we share the ability of changing the “number” variable in sample object to 100 different threads. So, this is what you called SHARED MUTABILITY.

To see why this is a devil’s work, just run the program. The expected output of that program is 4500.0, but as you run it repeatedly you will notice that, it doesn’t provide you the same answer. It will print different values below than 4500.0. Why this is because all the 100 threads are trying to access and increment the value of “number” at the same time, and some of the threads get destroyed due to collision.

Now you get the point, that is what meant by “Shared mutability is devil’s work”

In above program you can change the setNumber() method as below to prevent that is happening again.

That little ‘synchronized’ keyword does the job for you. Run the program again and you will see the output as 4500.0 and it will never change as you run the program for many times.

It’s always better to avoid creating things that allows to share it mutability. This thing is not necessary for tiny works, but when it comes to larger production level programs, you always need to create clean codes that doesn’t allow shared mutability.

--

--

Hashan Mahesh

I am an undergraduate at the University of Kelaniya, Sri Lanka. I study Information Communication Technology including Artificial Intelligence technologies