how to implement deep cloning without serialization?
i stumbled upon this problem....pls suggest some way
--- Update ---
i tried the following code ...but it is not working:
Code :
class DeepCopy implements Cloneable{
String par;
DeepCopy o1;
public static void main(String a[])
{
DeepCopy dummy=new DeepCopy();
dummy.go();
}
void go(){
o1=new DeepCopy();
o1.par="33";
DeepCopy o2=(DeepCopy)o1.clone();
System.out.println(o2.par);
}
public Object clone(){
try{
DeepCopy o2=(DeepCopy)super.clone();
o2.par=(String)o1.par.clone();
return o2;
}
catch(CloneNotSupportedException e)
{
return null;
}
}
}
Re: how to implement deep cloning without serialization?
Define 'it is not working'...does it compile? Are there exceptions? Does it misbehave? Post all errors in their entirety
Re: how to implement deep cloning without serialization?
Sorry for not defining clearly earlier......
in the line
Code Java:
o2.par=(String)o1.par.clone();
it is giving compiler error saying clone method has protected access in Object
But i have already overriden the clone method in the code above
Re: how to implement deep cloning without serialization?
String does not override the clone method, thus it relies on its parent class (Object) clone method, which is protected. Just create a new String from the old
Re: how to implement deep cloning without serialization?
I've edited your original post and have added [code] [/code] tags to allow the posted code to show its formatting, but see that even with the tags, the code you've posted was non-formatted. Please consider correcting that.
Re: how to implement deep cloning without serialization?
[B]Thanks for ur reply:
i tried out what u suggested:
Code Java:
import java.util.*;
class DeepCopy1 implements Cloneable{
String par;
DeepCopy1 o1,o2;
public static void main(String a[])
{
DeepCopy1 dummy=new DeepCopy1();
dummy.go();
}
void go(){
o1=new DeepCopy1();
o1.par="33";
o2=(DeepCopy1)o1.clone();
System.out.println(o2.par);
o1.par="44";
System.out.println(o2.par);
}
public Object clone(){
try{
DeepCopy1 o2=(DeepCopy1)super.clone();
o2.par=o1.par;
return o2;
}
catch(CloneNotSupportedException e)
{
return null;
}
}
}
Error i m getting is: null pointer exception at line:
o2.par=o1.par;
Re: how to implement deep cloning without serialization?
o1 is null. Where is it initialized? Why are you even using o1 here?
Re: how to implement deep cloning without serialization?
Sir,
o1 is initialized in the go method above....also i have made o1 an instance variable so that it can be accesed from any other method.
i want to make a deep clone of o1 and name it o2....so i m using o1.
Re: how to implement deep cloning without serialization?
And where do you call go() with relation to your cloning attempt? Is it called before the attempt? But more importantly why do you even need o1 when you are trying to copy when you can just use the field? You are ultimately trying to make a deep copy of DeepCopy1, not of o1.
Re: how to implement deep cloning without serialization?
Thanks for ur reply....with ur help, finally my code is working