Java by API/java.lang/Cloneable
implements Cloneable
/*
* Output:
x1: 10 20.98
x2: 10 20.98
*/
class MyClass implements Cloneable {
int a;
double b;
MyClass cloneTest() {
try {
return (MyClass) super.clone();
} catch(CloneNotSupportedException e) {
System.out.println("Cloning not allowed.");
return this;
}
}
}
public class MainClass {
public static void main(String args[]) {
MyClass x1 = new MyClass();
MyClass x2;
x1.a = 10;
x1.b = 20.98;
x2 = x1.cloneTest(); // clone x1
System.out.println("x1: " + x1.a + " " + x1.b);
System.out.println("x2: " + x2.a + " " + x2.b);
}
}