Possible Duplicate:
How do I copy an object in Java?
I've got a function in my class Claus
which calls a function to accept type Claus
. I'm currently passing in this
as the argument, however it's editing the current instance of the class which I don't want it to do. Instead, I'd like it to clone the current instance of the class and edit that, keeping the copies separate. How can I do that?
EDIT
Perhaps I should have clarified my question a little further...
I've got an object which is inside another object.. e.g Claus
and floss
. I've been reading up on shallow copy
vs deep copy
and I think I've got Claus
copying correctly. I'm doing it like so...
public Claus(Claus g){
cla = new Floss(g.getFloss());
//irrelevant other variables...
p = g.getP();
c = g.getC();
}
However, the function within Claus
which I'm declaring in the constructor the exact same way.. that is....
cla = new Floss(g.getFloss());
Where cla = the Floss
variable and g = Claus
which is being passed into the Constructor. The Floss
object doesn't seem to be creating a deep copy like it should. Why is this happening?