views:

13079

answers:

11

Consider the below code:

DummyBean dum = new DummyBean();
dum.setDummy("foo");
System.out.println(dum.getDummy()); // prints 'foo'

DummyBean dumtwo = dum;
System.out.println(dumtwo.getDummy()); // prints 'foo'

dum.setDummy("bar");
System.out.println(dumtwo.getDummy()); // prints 'bar' but it should print 'foo'

So, I want to copy the 'dum' to dumtwo' and I want to change 'dum' without affecting the 'dumtwo'. But the above code is not doing that. When I change something in 'dum', the same change is happening in 'dumtwo' also.

I guess, when I say dumtwo = dum, Java copies the reference only. So, is there any way to create a fresh copy of 'dum' and assign it to 'dumtwo' ?

A: 

To do that you have to clone the object in some way. Although Java has a cloning mechanism, don't use it if you don't have to. Create a copy method that does the copy work for you, and then do:

dumtwo = dum.copy();

Here is some more advise on different techniques for accomplishing a copy.

Yishai
A: 

Make a method on the object that returns a copy of the object.

Stefan Thyberg
+3  A: 

Yes. You need to Deep Copy your object.

bruno conde
As is, it's not even a copy at all.
Michael Myers
+2  A: 

Here's a decent explanation of clone() if you end up needing it...

http://en.wikipedia.org/wiki/Clone_%28Java_method%29

Jon Bringhurst
+9  A: 

Create a copy constructor:

class DummyBean {
  private String dummy;

  public DummyBean(DummyBean another) {
    this.dummy = another.dummy; // you can access  
  }
}

Every object has also a clone method which can be used to copy the object, but don't use it. It's way too easy to create a class and do improper clone method. If you are going to do that, read at least what Joshua Bloch has to say about it in Effective Java.

egaga
But then he'd have to change his code to DummyBean two = new DummyBean(one); Right?
Chris Kaminski
+3  A: 

Yes, you are just making a reference to the object. You can clone the object if it implement Cloneable.

Check out this wiki article about copying objects.

http://en.wikipedia.org/wiki/Object_copy#Copying_in_Java

Chrisb
+1  A: 

I disagree with your comment: // prints 'bar' but it should print 'foo' It works as it should. This is one of the advantages of Java over C++. If you want a copy, create a copy constructor or copy() method or override clone() to control the copying.

+1  A: 

Other than explicitly copying, another approach is to make the object immutable (no set or other mutator methods). In this way the question never arises. Immutability becomes more difficult with larger objects, but that other side of that is that it pushes you in the direction of splitting into coherent small objects and composites.

Tom Hawtin - tackline
A: 

You can try to implement Cloneable and use the clone() method however, if you use the clone method you should by standard ALWAYS override Object's public Object clone() method.

A: 

Just follow as below:

public class Deletable implements Cloneable{

    private String str;
    public Deletable(){
    }
    public void setStr(String str){
        this.str = str;
    }
    public void display(){
        System.out.println("The String is "+str);
    }
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

and where ever you wanna get another object, simple perform cloning. e.g:

Deletable del = new Deletable();
Deletable delTemp = del.clone(); // this line will return you an independent
                                 // object, the changes made to this object will
                                 // not be reflected to other object
Bhasker Tiwari
A: 

I can't believe this question got 6 kviews :)

And yeah, you need to clone the object, not copy the reference to the object...

botismarius