programming-examples/java/Core_Java/Cloning an Object.java
2019-11-15 12:59:38 +01:00

21 lines
500 B
Java

Cloning an Object
class MyClass implements Cloneable {
public MyClass() {
}
public Object clone() {
Cloneable theClone = new MyClass();
// Initialize theClone.
return theClone;
}
}
//Here's some code to create a clone.
MyClass myObject = new MyClass();
MyClass myObjectClone = (MyClass)myObject.clone();
//Arrays are automatically cloneable:
int[] ints = new int[]{123, 234};
int[] intsClone = (int[])ints.clone();