Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
public final class MutableClass {
  private Date d;
	
  public mutableClassMutableClass(Date d) {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
      //check permissions
    }
    this.d = d;
  }
	
  public Date getDate() {
    return this.d;	
  }
}

...

Code Block
bgColor#ccccff
public final class CloneClass implements Cloneable {
  private Date d;
	
  public cloneClassCloneClass(Date d) {
    SecurityManager sm = System.getSecurityManager();
      if (sm != null) {
	//check permissions
      }
      this.d = new Date(d.getTime());  //copy-in 
  }

  public Object clone() throws CloneNotSupportedException {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
      //check permissions
    }
    final cloneClassCloneClass cloned = (cloneClassCloneClass)super.clone();
    cloned.d = new Date( (Date)this.d.getTimeclone() );  //copy mutable Date object
    return cloned;
  }
	
  public Date getDate() {
    return new Date(this.d.getTime()); //copy and return
  }
}

...