ImmutableObject < Javapedia < TWiki

TWiki . Javapedia . ImmutableObject

Home | Help | Changes | Index | Search | Go

Immutable Object

Objects have identity (location in memory), state (data), and behavior (methods). Once constructed, an Immutable Object cannot change state.

Immutable objects greatly simplify your program, since they

Make a class immutable by following these guidelines :

Example

import java.util.Date;

/**
* Planet is an immutable class, since there is no way to change
* its state after construction.
*/
public final class Planet {
  /**
  * Primitive data is always immutable.
  */
  private final double mass;

  /**
  * An immutable object field. (String objects never change state.)
  */
  private final String name;

  /**
  * A mutable object field. In this case, the state of this mutable field
  * is to be changed only by this class. (In other cases, it makes perfect
  * sense to allow the state of a field to be changed outside the native
  * class; this is the case when a field acts as a "pointer" to an object
  * created elsewhere.)
  */
  private final Date dateOfDiscovery;

  /** Sole constructor */
  public Planet (double mass, String name, Date dateOfDiscovery) {
     this.mass = mass;
     this.name = name;
     //make a private copy of dateOfDiscovery
     //this is the only way to keep the dateOfDiscovery
     //field private, and shields this class from any changes
     //to the original dateOfDiscovery object
     this.dateOfDiscovery = new Date(dateOfDiscovery.getTime());
  }

  //gets but no sets, and no methods which change state
  public double getMass() {
    return mass;
  }

  public String getName() {
    return name;
  }

  /**
  * Returns a defensive copy of the field.
  * The caller of this method can do anything they want with the
  * returned Date object, without affecting the internals of this
  * class in any way.
  */
  public Date getDateOfDiscovery() {
    return new Date(dateOfDiscovery.getTime());
  }
}

-- JohnOHanley - 19 Jul 2003


Also see


Discussion about ImmutableObject

(An editor apparently disagrees strongly with PrivateMembersLast...)

-- JohnOHanley - 23 Jul 2003

----- Revision r6 - 20 Jul 2004 - 08:16:45 - Main.hlovatt