How do I clone a java bean object?
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.