Singleton IntList
¥import java.util.NoSuchElementException;
¥
¥abstract class IntList {
¥  abstract int getFirst();
¥  abstract IntList getRest();
¥}
¥
¥class Empty extends IntList {
¥  static final Empty ONLY = new Empty();
¥  private Empty() {}
¥  int getFirst() {
¥    throw new
¥      NoSuchElementException("getFirst applied to Empty()");
¥  }
¥  int getRest() {
¥    throw new
¥      NoSuchElementException("getRest applied to Empty()");
¥  }
¥  public String toString() { return "Empty()"; }
¥}
¥
¥class NonEmpty extends IntList {
¥  int first;
¥  IntList rest;
¥  int getFirst() { return first; );
¥  IntList getRest() { return rest; }
¥  public String toString() { return "NonEmpty(" + first + ", " + rest + ")"; }
¥}
¥ 
¥
Static member holding the unique instance
Private constructor