Immutable objects and mutable objects with an example
In object oriented programming language, in short OOPS, every problem, precisely every requirement is viewed as an object. Understanding the problem in this point of view brings many good things possible in programming like encapsulation, abstraction, polymorphism and inheritance. Anyways this post is not intended for describing benefits of OOPS.
Object could be any thing. Let us take Car for an instance. Basically car is an object and it has name, wheels, metal body, engine, maximum speed, gear box etc as properties.
Let us consider the car name is Ferrari, maximum speed is 200 km per hr with 6 gears. While driving the car driver can change the speed of the car and change the gear etc.
So while car is running it's current speed, current gear are considered to be state of the object.
The current speed can be changed using accelerator and gear can be changed using gear box. These can be considered as behavior of the car object. Of course car has a name as well.
Mutable and Immutable objects
Below program creates immutable objects:
01 public final class ImmutableObject {
02
03 private String name;
04
05 private int maxSpeed;
06
07 private String color;
08
09 /**
10 * @param name
11 * holds car name
12 * @param maxSpeed
13 * holds maximum speed of the car
14 * @param color
15 * holds color of car
16 */
17 public ImmutableObject(String name, int maxSpeed, String color) {
18 this.name = name;
19 this.maxSpeed = maxSpeed;
20 this.color = color;
21 }
22
23 public String getName() {
24 return name;
25 }
26
27 public int getMaxSpeed() {
28 return maxSpeed;
29 }
30
31 public String getColor() {
32 return color;
33 }
34
35 public static void main(String[] args) {
36 ImmutableObject ferrari = new ImmutableObject("Ferrari", 200, "Red");
37 System.out.println("Car name: " + ferrari.getName());
38 System.out.println("Car color:" + ferrari.getColor());
39 System.out.println("Car maximum speed:" + ferrari.getMaxSpeed());
40 System.out.println();
41
42 ImmutableObject anotherCar = new ImmutableObject("BMW", 180, "White");
43 System.out.println("Car name: " + anotherCar.getName());
44 System.out.println("Car color:" + anotherCar.getColor());
45 System.out.println("Car maximum speed:" + anotherCar.getMaxSpeed());
46 }
47 }
48
Output:
Car name: FerrariCar color:Red
Car maximum speed:200
Car name: BMW
Car color:White
Car maximum speed:180
Explanation
Line 1: First and fore most rule is to make your class final class.
Line 3 to Line 7: All variables are private variables. private access modifier restrics usage of members within the class
Note: Do not use other classes as data types of your instance variables which are mutable in behavior. All of our example variables are of String type. String is immutable. See API document of String class for more information.
Line 17: Creates parameterised constructor with state which can not be changed at run time
Line 23 to Line 33: Create only those methods which shares it's state to the other classes. As we did in our example ImmutableObject. Allows only getter methods and no setter methods at all.
Line 36 to Line 42: Finally, create 2 objects ferrari and another car with different state which can not be changed at run time..
02 public class MutableObject {
03
04 private String name;
05
06 private int maxSpeed;
07
08 private String color;
09
10 private int currentGear;
11
12 private int currentSpeed;
13
14 /**
15 * @param name
16 * holds car name
17 * @param maxSpeed
18 * holds maximum speed of the car
19 * @param color
20 * holds color of car
21 */
22 public MutableObject(String name, int maxSpeed, String color) {
23 this.name = name;
24 this.maxSpeed = maxSpeed;
25 this.color = color;
26 }
27
28 public String getName() {
29 return name;
30 }
31
32 public int getMaxSpeed() {
33 return maxSpeed;
34 }
35
36 public int getCurrentGear() {
37 return currentGear;
38 }
39
40 public void setCurrentGear(int currentGear) {
41 this.currentGear = currentGear;
42 }
43
44 public int getCurrentSpeed() {
45 return currentSpeed;
46 }
47
48 public void setCurrentSpeed(int currentSpeed) {
49 this.currentSpeed = currentSpeed;
50 }
51
52 public String getColor() {
53 return color;
54 }
55
56 public static void main(String[] args) {
57 MutableObject ferrari = new MutableObject("Ferrari", 200, "Red");
58 System.out.println("Car name: " + ferrari.getName());
59 System.out.println("Car colo: " + ferrari.getColor());
60 System.out.println("Car maximum speed:" + ferrari.getMaxSpeed());
61
62 ferrari.setCurrentGear(5);
63 ferrari.setCurrentSpeed(150);
66
67 ferrari.setCurrentGear(1);
68 ferrari.setCurrentSpeed(10);
69 System.out.println(String.format("Changed state at run time!.. \nCurrent speed is %s and current gear is %s ", ferrari.getCurrentSpeed(), ferrari.getCurrentGear()));
73
74 }
75}
76
Output:
Explanation
Few more details about immutable objects:
- It is always good practice to use immutable objects in our application especially in concurrent apps where ever applicable
- Immutable objects ensures correct results/output always in all environments like multi threading environment, collection framework.
- HashMap<key, value> always requires immutable object as key. Otherwise it behaves in undetermined way.
- Consider cloning when creating immutable classes. I will post another article about importance of deep cloning and shallow copy in details in near future.
- Cache memory implementation uses immutable objects
- java.lang.String is the most used immutable class in java

