I don\'t understand the mechanism of cloning custom object. For example:
public class Main{
public static void main(String [] args) {
Person pe
The JVM is able to clone the object for you, and you're thus not supposed to construct a new person by yourself. Just use this code:
class Person implements Cloneable {
// ...
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
or
class Person implements Cloneable {
// ...
@Override
public Object clone() {
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
throw new Error("Something impossible just happened");
}
}
}
This will work even if the Person class is subclassed, whereas your clone implementation will always create an instance of Person (and not an instance of Employee for an Employee, for example).