Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cloning a java object..

Status
Not open for further replies.

patnim17

Programmer
Jun 19, 2005
111
US
Hi,
I have a Java object (ThirdPartyClass) that does not have the clone() method exposed. Is there a simpler way in java to clone or in other words get a copy of this object?

pat
 
Unless the clone method is implemented, I don't know any other way. But if you have the object, why would you need a copy?

Cheers,
Dian
 
You don't get a clone, you get a reference to the same object.

Stefan wrote a nice post about this recently.

Cheers,
Dian
 
So i guess he'd need a clone if he needed to manipulate the object in some way for another result yet not want to make the changes permanent.

I had to do quite a bit of this in a past project.
 
Could be, but in that case I'd examine the reasons for that and think if I'm doing it ok. When you find something hard to do in Java, generally is because you didn't focus the problem in the rigth way.

Cheers,
Dian
 
true true.

I would suggest putting a method on the object that literally returned a new copy of itself. is this feasable?
 
The OP did say it was a 3rd party class so adding a new method is probably out of the question.

Tim
 
Well, you can write an object cloner via reflection, but it's an ugly and not so ellegant solution.

Cheers,
Dian
 
Yeah if the class isn't finalized then you could subclass and reflect itself back to the caller. But tim is right, this is a third party component, my bad.

I am totally curious as to why the clone needs to exist.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top