Sunday, 4 August 2013

Copy constructor using Java

callcopcons.java
class copyconstructor
{
int a,b;
copyconstructor(){}
copyconstructor(copyconstructor objecttoclone){
a=objecttoclone.a;
b=objecttoclone.b;
}

void SetA(int a,int b){
this.a=a;
this.b=b;
}
void GetA(){
System.out.println("A="+a);
System.out.println("B="+b);
}
}
class callcopcons
{
public static void main(String args[]){
copyconstructor AA=new copyconstructor();
AA.SetA(50,60);
copyconstructor BB=new copyconstructor();
copyconstructor CC=new copyconstructor();
CC.GetA();
BB.GetA();
AA.GetA();
CC=AA;
CC.GetA();
}
}

Output:
A=0
B=0
A=0
B=0
A=50
B=60
A=50
B=60

Thanks for watching...comment it.

1 comment: