/** * This is to demonstrate cloning. * ToClone has one int field called "field" which showField() prints. * SimpleCloneMe() uses the default via super.clone() * and fancyCloneMe() uses a custom clone() method. * To understand this test you'll also have to study the actual * cloning source in ToClone.java. * * @author Bob Futrelle * @version 1.0 7/12/2002 * */ public class CloneTest { static ToClone tcc, ttc, ttcc; public static void main(String[] args) throws CloneNotSupportedException { ToClone tc = new ToClone(); tc.field = 33; say("Value in object to be cloned"); tc.showField(); tcc = (ToClone)tc.simpleCloneMe(); // do simple cloning say("Value in a simple clone"); tcc.showField(); tcc.field = 44; say("We change the value in the clone."); tcc.showField(); say("But the original is unchanged"); tc.showField(); ttc = (ToClone)tc.fancyCloneMe(); say("This uses this.clone() rather than the super.clone().\n" + " The value set in the overridden clone method is now there."); ttc.showField(); ttc.field = 55; say("The new clone value is changed"); ttc.showField(); say("and the original is still unchanged."); tc.showField(); ttcc = tc.goryCloneMe(); say("This creates a brand new object and manually copies the value + 3\n" + " from the current object to the new ."); ttcc.showField(); } // main() static void say(String s) { System.out.println(s); } }