Pages

04 May 2012

Mutable and Immutable Classes in Java

"In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created."

In Java, String is popular with being immutable class that had forced me understand what certainly immutable class is. The definition of being immutable object depends on its state; but what does state depend on or how can we protect an object's state? Lets look at the following class:

public class Immutable {
  private long createdTime = System.currentTimeMillis();

  @Override
  public String toString() {
    return "Immutable:" + this.createdTime;
  }
}

The state of a class refers to keeping instance variables(attributes) constant. As in the example; this class has a single attribute that holds the created time-stamp in primitive long type and has no public method that can change any attribute(createdTime) of this class. So far, this class suites the definition of being immutable.

String class supplies many operations like concat, substring etc. All those well defined methods on String class create a new String object that holds new value. At the following example, line1 will print "oguz" and line2 will print our "oguzhan", because concat operation create a new string as a result.

public class TestInteger {
  public static void main(String... args) {
    String s1 = "oguz";
    String s2 = s1.concat("han");
    System.out.println("s1=" + s1); // line 1
    System.out.println("s2=" + s2); // line 2
  }
}


There are also other immutable fundamental classes in Java ecosystem; but their immutability is not specifically underpinned in API documentations. For example, java.lang.Integer class is wrapper class of primitive "int" and its state cannot be changed after its craeted through its public methods(interface).

public class TestInteger {
  public static void main(String... args) {
    Integer i = new Integer(5);
    Object o = i;
    i++;
    System.out.println(o.toString() + "," + i); // line X
  }
}

Above code snippet will print out "5,6", because "++" operand will auto unbox the Integer object i to int then increase it by one. Finally, autobox the result of operation to a new Integer object. There is no way out to change a created Integer instance state. Strangely, it is not famous with this property.

A simple class, that has at least one setter (or in other words mutator) method for any of its attributes, will be counted as a Mutable class. As in the example:

public class Mutability {

  private int number; 
  private String name;
  private int sector;

  public Mutability(String name, int sector) {
    super();
    this.name = name;
    this.sector = sector;
  }

  public void setNumber(int number) { // makes this class mutable...
    this.number = number;
  }

  public int getNumber() {
    return this.number;
  }

  @Override
  public String toString() {
    return name + "#" + sector + "[" + number + "]";
  }  
}

Take care...
Thx to Gzm...