+ 1
A copy constructor is an easy way to copy data from one object to anther of the same type without coping the references.
copy constructor of a class called Person should have a parameter of Person class :
public Person(Person p){
this(p.getAge());
}
Below is an example :
class Person{
private int age;
public Person(int age){
this.age = age;
}
// * Copy constructor *
public Person(Person p){
this(p.getAge());
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
public class Program
{
public static void main(String[] args) {
Person p1 = new Person(17);
Person p2 = new Person(p1);
p1.setAge(25);
System.out.println("p1 age = " +p1.getAge());
System.out.println("p2 age = " +p2.getAge());
}
}



