Results 1 to 7 of 7
Thread: Object copy
- 12-17-2008, 08:41 AM #1
Member
- Join Date
- Mar 2008
- Posts
- 6
- Rep Power
- 0
Object copy
Hi all, if someone could plz help, been crashing my head with this problem for too long.
I have a question regarding making a copy of an object. I've got an object which i need to modify. I want to make a copy of that object so i can change it's contents and still have a "backup" object (copy). When I make a copy it still references the original object. When i change something in the main object it is also changed in the copy. How can I make a "standalone" copy which will serve me as a backup...Last edited by Oktam; 12-17-2008 at 09:18 AM.
- 12-17-2008, 09:03 AM #2
please try this
newObject = yourExistingObject.clone();
By convention, the object returned by this method should be independent of this object (which is being cloned).DevZ;)
- 12-17-2008, 09:21 AM #3
Member
- Join Date
- Mar 2008
- Posts
- 6
- Rep Power
- 0
I just tried, doesn't work, i guess because the classes themselves hold classess inside.
-
You have to read up on the use of the Cloneable interface and how to use it. You need your class and the classes of all objects it holds to implement this interface, and each of these classes should override the clone method making it public. Then inside of your class's clone method you need to call clone() on the objects it holds.
- 12-17-2008, 01:50 PM #5
Member
- Join Date
- Mar 2008
- Posts
- 6
- Rep Power
- 0
Thanks, problem solved, I wrote another constructor with the object as a parameter which then i used to copy the existing object into the new one.
Thanks for the advices :)
- 12-17-2008, 05:08 PM #6
you could create a new object using all the values of your current object. use a bunch of "getter" methods methods to do this and then store them in a backup array.
This is the "easy" way of doing it. Using the Cloneable interface is most likely the correct way to do it though.
- 07-21-2010, 08:09 AM #7
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
Object copy
Hi,
Cloning is a shallow copy of the original object. If the cloned object is changed, the original object will be changed accordingly. To avoid such side effect, you may use a deep copy instead of a shallow copy. eg: A class doesn't implement Cloneable interface.
public MyClass clone() {
return new MyClass();
}
Similar Threads
-
Operator < cannot be applied to java.lang.Object, Object
By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 02:12 AM -
USB Device Copy
By Mir in forum New To JavaReplies: 3Last Post: 08-25-2008, 11:44 AM -
Parsing a superclass object to subclass object dynamicly
By Andrefs in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 04:27 PM -
How can i copy a folder from one place to another..
By rajeshgubba in forum New To JavaReplies: 4Last Post: 06-14-2008, 02:21 AM -
Problem with array Copy
By coco in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks