Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do I clone a java bean object?

807603Nov 25 2007
I have a java bean object that I want to clone. I already use XMLEncoder to save its data to a file -- to implement save functionality in the program. I thought about using (or abusing) XML encoder to create a clone of the same class. Essentially, I would XML Encode the object to a bytearray and then XMLdecode it back as another instance of the object. Another alternative would be to use the serializable interface to copy the object. But I figure that there must be a better solution, even though the case is rare and will not create a performance problem. I know that there is a method in an apache.common.beanutils class which does clone bean objects. I could use that, but I do not want to include an additional library for such as simple task (this is for an assignment and submitting an extra package will be akward). Anyway, I figure that cloning java beans is done all the time and there must be a way to do this. I am using java 6.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 23 2007
Added on Nov 25 2007
0 comments
310 views