0

how to copy one object to another object without or with using copy constructor?

25th Aug 2016, 7:39 PM
Purbasha Mitra
Purbasha Mitra - avatar
4 Antworten
+ 1
This could be by copy constructor. Suppose the class is Person, its copy constructor could be: public Person(Person p){ this(p.getAge()); } have a look at example in below: ex: 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()); } }
25th Aug 2016, 9:17 PM
Tiger
Tiger - avatar
0
objects reference
25th Aug 2016, 7:59 PM
sathya
0
plz give an example...
25th Aug 2016, 8:41 PM
Purbasha Mitra
Purbasha Mitra - avatar
0
thnx...
25th Aug 2016, 9:19 PM
Purbasha Mitra
Purbasha Mitra - avatar