HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

JAVA Interview Questions 26 - 27
Back to the list of all Java Interview Questions

Question 26: How will you write an immutable class?
Writing an immutable class is generally easy but there can be some tricky situations. Follow the following guidelines:
  • A class is declared final (i.e. final classes cannot be extended).
    public final class MyImmutable { … }
  • All its fields are final (final fields cannot be mutated once assigned).
    private final int[] myArray; //do not declare as -> private final int[] myArray = null;
  • Do not provide any methods that can change the state of the immutable object in any way – not just setXXX methods, but any methods which can change the state.
  • The “this” reference is not allowed to escape during construction from the immutable class and the immutable class should have exclusive access to fields that contain references to mutable objects like arrays, collections and mutable classes like Date etc by:
    • Declaring the mutable references as private.
    • Not returning or exposing the mutable references to the caller (this can be done by defensive copying)
Wrong way to write an immutable class Right way to write an immutable class
Wrong way to write a constructor: Right way is to copy the array before assigning in the constructor.

public final class MyImmutable

{

   private final int[] myArray;

   public MyImmutable(int[] anArray)

   {

      this.myArray = anArray; // wrong

   }

   public String toString()

   {

      StringBuffer sb = new StringBuffer("Numbers are: ");

      for (int i = 0; i < myArray.length; i++)

      {

          sb.append(myArray[i] + " ");

      }

      return sb.toString();

   }

} 

public final class MyImmutable

{

   private final int[] myArray;

   public MyImmutable(int[] anArray)

   {

      this.myArray = anArray.clone(); // defensive copy

   }

   public String toString()

   {

      StringBuffer sb = new StringBuffer("Numbers are: ");

      for (int i = 0; i < myArray.length; i++)

      {

         sb.append(myArray[i] + " ");

      }

      return sb.toString();

   }

}

// the caller could change the array after calling the constructor. // the caller cannot change the array after calling the constructor.

int[] array = {1,2};

MyImmutable myImmutableRef = new MyImmutable(array) ;

System.out.println("Before constructing " + myImmutableRef);

array[1] = 5; // change (i.e. mutate) the element

System.out.println("After constructing " + myImmutableRef);

int[] array = {1,2};

MyImmutable myImmutableRef = new MyImmutable(array) ;

System.out.println("Before constructing " + myImmutableRef);

array[1] = 5; // change (i.e. mutate) the element

System.out.println("After constructing " + myImmutableRef);

Out put:
Before constructing Numbers are: 1 2
After constructing Numbers are: 1 5

As you can see in the output that the “MyImmutable” object has been mutated. This is because the object reference gets copied
Out put:
Before constructing Numbers are: 1 2
After constructing Numbers are: 1 2

As you can see in the output that the “MyImmutable” object has not been mutated.

Wrong way to write an accessor. A caller could get the array reference and then change the contents

Right way to write an accessor by cloning.

public int[] getArray()
{

   return myArray;

}

public int[] getAray()

{

   return (int[]) myArray.clone();

}

Important: Beware of using the clone() method on a collection like a Map, List, Set etc because they are not only difficult to implement correctly but also the default behavior of an object’s clone() method automatically yields a shallow copy. You have to deep copy the mutable objects referenced by your immutable class.

Question 27: How would you defensively copy a Date field in your immutable class?

public final class MyDiary
{

   private Date myDate = null;

   public MyDiary(Date aDate)

   {

      this.myDate = new Date(aDate.getTime()); // defensive copying by not exposing the “myDate” reference

   }

   public Date getDate()

   {

      return new Date(myDate.getTime); // defensive copying by not exposing the “myDate” reference

   }

}



Back to the list of all Java Interview Questions