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?

5
  • 1
    Could you provide code to illustrate what you are doing? Commented Dec 12, 2011 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. Commented Dec 12, 2011 at 22:44
  • 5
    Man it's like attack of the clones in here. Commented Dec 12, 2011 at 22:45
  • I've provided an update on my question to help clarify things. Commented Dec 12, 2011 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? Commented Dec 13, 2011 at 0:00

8 Answers 8

4

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

+1 for teaching me something :)
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.

Comments

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

7 Comments

I don't think the constructor of Claus will look like this.
Copy constructor in Java ?
Is that bad style in Java? Sorry I come from a C++ background but would love to learn about Java standards.
There is no built-in copy constructor in Java
Ahh built in. I wasn't aware of that and added that he should define his own. Thanks for the tip! Is it considered bad style to define your own copy ctor instead of using clone?
|
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.

1 Comment

Only works if the type of obj implements Cloneable. Otherwise you get CloneNotSupportedException.
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..

Comments

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.