Organizational Research By

Surprising Reserch Topic

Immutable class:How to create Immutable Class and Object in Java with Immutable Class Example in Java

asked Sep 13, 2013 in Java Interview Questions by rajesh
edited Sep 12, 2013 by rajesh
0 votes

Related Hot Questions

1 Answer

0 votes
Immutable Class Example in Java
Here is complete code example of writing immutable class in Java. We have followed simplest approach and all rules for making a class immutable, including it making class final to avoid putting immutability at risk due to Inheritance and Polymorphism.
public final class Contacts {
    private final String name;
    private final String mobile;
    public Contacts(String name, String mobile) { = name; = mobile;
    public String getName(){
        return name;
    public String getMobile(){
        return mobile;
This Java class is immutable, because its state can not be changed once created. You can see that all of it’s fields are final. This is one of the most simple way of creating immutable class in Java, where all fields of class also remains immutable like String in above case. Some time you may need to write immutable class which includes mutable classes like java.util.Date, despite storing Date into final field it can be modified internally, if internal date is returned to the client. In order to preserve immutability in such cases, its advised to return copy of original object, which is also one of the Java best practice. here is another example of making a class immutable in Java, which includes mutable member variable.
public final class ImmutableReminder{
    private final Date remindingDate;
    public ImmutableReminder (Date remindingDate) {
        if(remindingDate.getTime() < System.currentTimeMillis()){
            throw new IllegalArgumentException("Can not set reminder” +
                        “ for past time: " + remindingDate);
        this.remindingDate = new Date(remindingDate.getTime());
    public Date getRemindingDate() {
        return (Date) remindingDate.clone();
In above example of creating immutable class, Date is a mutable object. If getRemindingDate() returns actual Date object than despite remindingDate being final variable, internals of Date can be modified by client code. By returning clone() or copy of remindingDate, we avoid that danger and preserves immutability of class.
Benefits of Immutable Classes in Java
As I said earlier Immutable classes offers several benefits, here are few to mention:
1) Immutable objects are by default thread safe, can be shared without synchronization in concurrent environment.
2) Immutable object simplifies development, because its easier to share between multiple threads without external synchronization.
3) Immutable object boost performance of Java application by reducing synchronization in code.
4) Another important benefit of Immutable objects is reusability, you can cache Immutable object and reuse them, much like String literals and Integers.  You can use static factory methods to provide methods like valueOf(), which can return an existing Immutable object from cache, instead of creating a new one.
Apart from above advantages, immutable object has disadvantage of creating garbage as well. Since immutable object can not be reused and they are just a use and throw. String being a prime example, which can create lot of garbage and can potentially slow down application due to heavy garbage collection, but again that's extreme case and if used properly Immutable object adds lot of value.
That's all on how to write immutable class in Java. we have seen rules of writing immutable classes, benefits offered by immutable objects and how we can create immutable class in Java which involves mutable fields. Don’t forget to read more about concurrency benefit offered by Immutable object in one of the best Java book recommended to Java programmers, Concurrency Practice in Java.
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013