Hi how can i create a copy of an object? (cloning???) my requirement is to create a copy of an object. after that when i change some attributes of the original object; the copy of that object shall remain unchanged (ie it shall contain the older values of the attribute) can anybody guide me in that with proper syntax?? tia jignesh ------------------
"patel_o", The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements. Thanks.
The solution implementing Cloneable is one way, the other is a constructor with the object itself, for example:
It is a matter of taste (and maybe time), which one you prefer. The "Cloneable"-solution is somewhat more intuitive to write, whereas the "Constructor"-solution is somewhat more intuitive to read. But as said: "A matter of taste." Ok, sometimes the "Constructor"-solution is sometimes hard to implement, depending of members. Best regards Detlev