2

Possible Duplicate:
How do I copy an object in Java?

I've got a function in my class Claus which calls a function to accept type Claus. I'm currently passing in this as the argument, however it's editing the current instance of the class which I don't want it to do. Instead, I'd like it to clone the current instance of the class and edit that, keeping the copies separate. How can I do that?

EDIT

Perhaps I should have clarified my question a little further...

I've got an object which is inside another object.. e.g Claus and floss. I've been reading up on shallow copy vs deep copy and I think I've got Claus copying correctly. I'm doing it like so...

public Claus(Claus g){
    cla = new Floss(g.getFloss());
            //irrelevant other variables...
    p = g.getP();
    c = g.getC();
}

However, the function within Claus which I'm declaring in the constructor the exact same way.. that is....

 cla = new Floss(g.getFloss());

Where cla = the Floss variable and g = Claus which is being passed into the Constructor. The Floss object doesn't seem to be creating a deep copy like it should. Why is this happening?

Community
  • 1
  • 1
user596186
  • 579
  • 1
  • 7
  • 16
  • 1
    Could you provide code to illustrate what you are doing? – artdanil Dec 12 '11 at 22:43
  • Dose Claus implement Cloneable? What if you pass in a clone? What your describing doesn't have a bad code smell per se, but perhaps a very slight odor. I wonder if you're going about things wrong. Perhaps you'll be better off telling us what behavior you're trying to achieve and not how you're trying in code to achieve it. – Hovercraft Full Of Eels Dec 12 '11 at 22:44
  • 5
    Man it's like attack of the clones in here. – Paul Bellora Dec 12 '11 at 22:45
  • I've provided an update on my question to help clarify things. – user596186 Dec 12 '11 at 23:50
  • "The Floss object doesn't seem to be creating a deep copy like it should." Can you give more details, what evidence do you have that it isn't properly making a deep copy? – Kevin K Dec 13 '11 at 00:00

8 Answers8

4

Implement Cloneable and use this.clone(), just like any other variable.

Kevin
  • 53,822
  • 15
  • 101
  • 132
3

You should take a look at the method Object#clone

http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#clone%28%29

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
3

Consider implementing a copy constructor for your class. This is a constructor that takes another instance of the same type and copies its state into the new object.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
1
 Object objClone = obj.clone();

Clone will return just like it sounds, a clone of your object. Clone() is a method from the Object super class, so all of your objects will have access to this method if you implement the Cloneable interface.

BVSmallman
  • 601
  • 3
  • 9
1
void foo(Claus c)
{
}

Rather than foo(this), do foo(new Claus(this))

Edits:

And then define a copy constructor in Claus like so:

public Claus(Claus c)
{
    this.whatever = c.whatever;
    // etc.
}
Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
1

You might want to implement the .clone() method. But, you have to make sure it implements the Cloneable interface.

But it's suggested to prefer other methods like Copy Constructor and static-factory-method that takes an object as an argument and creates a new instance that's a copy of the passed object.

But, one thing that you really need to be aware of is that whether just a shallow copy of all the fields is enough or do you need a deep copy. Because, in case you need a deep copy and created a shallow copy of your object might mean a potential bug in your code.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
1

You can do this:

public class MyObject {
    //attributes
   private String attr1;

   public MyObject() {} //first construct

   public MyObject(MyObject obj) { 
       //do copy of all attributes 
       this.attr1 = obj.getAttr1();
   }

   //setters and getters

   public void method(MyObject obj) {
      MyObject obj2 = new MyObject(obj);
      //your processing
   }

}

and call it

MyObject obj = new MyObject();
obj.setAttr1("someone");
obj.method(obj);

Hope this help..

Anderson Carniel
  • 1,711
  • 2
  • 16
  • 22
-1

As others have said, you should either implement the clone() method or create a copy constructor.

Whichever solution you choose, you will want to make what is called a "deep copy". If the object is simple, than creating a deep copy is simple. If the object is complicated, one trick is to serialize and deserialize the object. This will make a deep copy with a minimum amount of effort.

Whenever I do this I like to use XStream instead of native Java serialization. It serializes an object into XML, including any nested objects and collections, which you can then deserialize for a deep copy. The following code will most likely work for your class with no modifications:

public Claus clone(Claus source) {
      XStream xstream = new XStream();
      String serializedObj = xstream.toXML(source);
      return (Claus) xstream.fromXML(serializedObj);
   }
Jordan Bentley
  • 1,309
  • 1
  • 8
  • 28