Results 1 to 7 of 7

Thread: Object copy

  1. #1
    Oktam is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Default 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.

  2. #2
    DevzAbhi's Avatar
    DevzAbhi is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    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;)

  3. #3
    Oktam is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Default

    I just tried, doesn't work, i guess because the classes themselves hold classess inside.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  5. #5
    Oktam is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Default

    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 :)

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  7. #7
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default 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

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. USB Device Copy
    By Mir in forum New To Java
    Replies: 3
    Last Post: 08-25-2008, 11:44 AM
  3. Replies: 1
    Last Post: 07-22-2008, 04:27 PM
  4. How can i copy a folder from one place to another..
    By rajeshgubba in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 02:21 AM
  5. Problem with array Copy
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •