![]() |
VOOZH | about |
Java is a popular object-oriented programming (OOP) language and it's code design revolves around it's objects and their manipulation. One of the key concepts in this OOP paradigm is the classification of objects into mutable and immutable types. These classifications say whether an object's state can be modified after its creation or not. This is very important concept to understand the behavior and design of Java programs.
Pre-requisite: Object Oriented Programming (OOPs) Concept in Java
Note: In this article, an object's state at a particular instant means the snapshot of all of the values of that object's data members (or fields) at that particular point of time.
This article explains the definition, example, code, comparison, and advantages of Mutable and Immutable Objects in Java.
Mutable class objects are those whose state can be modified after initialization. This means the values of their fields can be changed, add or remove elements from the collections they contain, ie generally alter their internal state. Examples of mutable objects are Java's StringBuilder, StringBuffer, & java.util.Date.
In mutable objects, changes to the object's state (data members) do not result in the creation of a new object. Instead, the existing object's values are modified through functions provided by the object's class, such as setters. Mutable objects have both getters and setters functions.
On the other hand, immutable class objects are those whose state cannot be modified after initialization. Once created, the values and state of an immutable object remain fixed throughout its scope. Examples of immutable objects are primitive types like int, long, float, double, as well as classes like String. all legacy classes and wrapper classes are Immutable classes.
Immutable objects, by their nature, do not provide methods/functions to modify their state. Instead, they only offer getters to retrieve their values and no setters function. Any attempt to change an immutable object's state results in the creation of a new object with the modified values.
Mutable class coding:
John Doe
X: 5, Y: 10
Here are summary of important differences.
Mutable | Immutable |
|---|---|
can change the value of the object after initialization. | cannot change the values of object after initialization |
State can be changed | State remains constant after creation |
No new objects are formed | Changing the value of the object creates a new object |
Provides methods to change object | Does not provide methods to change object value |
Supports get() and set() methods | Only supports get() method to retrieve object value |
May or may not be thread-safe | Immutable objects are inherently thread-safe |
Essentials for creating a mutable class include methods for modifying fields, getters, and setters | Essentials for creating an immutable class include final class, private fields, and final mutable objects |
| Mutable Objects Advantages | Immutable Objects Advantages |
|---|---|
| Flexibility: Mutable class objects allow modification of their state, which is useful for dynamic changes. | Thread Safety: Immutable objects can be shared across threads without synchronization since their state cannot change. |
| Performance: Often perform well since they can be modified directly without creating new objects. | Security: Immutable objects are more secure as their state cannot be altered maliciously after creation. |
| Builder Pattern: Mutable objects are commonly used with the builder pattern, allowing fluent and readable object creation. | Simpler State Management: Immutable objects simplify debugging and maintenance because their state does not change unexpectedly. |
| Caching: Ideal for caching mechanisms as their state does not change, making them easily cacheable. | |
| Memory Management: Easier memory management since they do not require defensive copying. |