How to create Immutable Class and Object in Java - Tutorial Example
Writing or creating immutable classes in Java is becoming popular day by day, because of the concurrency and multithreading advantages provided by immutable objects. Immutable objects offer several benefits over a conventional mutable object, especially while creating concurrent Java applications. An immutable object not only guarantees the safe publication of an object’s state but also can be shared among other threads without any external synchronization. In fact, JDK itself contains several immutable classes like String
, Integer
, and other wrapper classes.
For those who don’t know, immutable objects are those whose state cannot be changed once created. A good example is java.lang.String
— once created, a String
object cannot be modified. Any operation that seems to modify a String
object (like trim()
, toUpperCase()
, etc.) actually results in a new object.
What is an Immutable Class in Java?
An immutable class is one whose object cannot be modified once created. Any modification will result in a new immutable object. The best examples to understand this concept are String
(immutable) and StringBuffer
(mutable). Here are some important characteristics of immutable classes:
- The state of an immutable object cannot be modified after construction.
- All fields of the immutable class should be
final
. - Object references must not leak during the construction process.
- The class should be declared
final
to prevent inheritance.
How to Write an Immutable Class in Java?
Below are the key rules to create an immutable class in Java:
- The state of the object cannot be modified after construction.
- All fields should be
final
to ensure they are assigned only once. - Ensure proper construction to prevent object reference leaks.
- Declare the class
final
to avoid subclassing, which could break immutability.
Let's look at a simple example of creating an immutable class:
Example of Immutable Class
public final class Contacts {
private final String name;
private final String mobile;
public Contacts(String name, String mobile) {
this.name = name;
this.mobile = mobile;
}
public String getName() {
return name;
}
public String getMobile() {
return mobile;
}
}
In this example, the Contacts
class is immutable because its state cannot be changed after the object is created. Both the fields (name
and mobile
) are final
and assigned only once inside the constructor.
Handling Mutable Fields
Sometimes, you may need to include mutable fields (like Date
) in an immutable class. In such cases, returning a copy of the mutable object is recommended to preserve immutability.
public final class ImmutableReminder {
private final Date remindingDate;
public ImmutableReminder(Date remindingDate) {
if (remindingDate.getTime() < System.currentTimeMillis()) {
throw new IllegalArgumentException("Cannot set a reminder for past time: " + remindingDate);
}
this.remindingDate = new Date(remindingDate.getTime());
}
public Date getRemindingDate() {
return new Date(remindingDate.getTime());
}
}
In this example, Date
is a mutable object, but we preserve immutability by returning a clone of the Date
object instead of returning the actual reference.
Benefits of Immutable Classes in Java
- Immutable objects are inherently thread-safe and can be shared without synchronization in a concurrent environment.
- They simplify development by removing the need for synchronization.
- Immutable objects can be reused and cached for better performance.
- They play a key role in writing functional programming code in Java.
However, immutability also has some downsides, like creating additional garbage due to the need for new objects whenever modifications are required.
Conclusion
In this article, we explored how to create immutable classes in Java, discussed their properties, and examined the benefits and challenges of using immutable objects. If you're working on concurrent applications, leveraging immutability can greatly simplify your code while improving safety and performance.
Comments
Post a Comment