How to Copy an Object in Java

How do I copy an object in Java?

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.

how do i create a copy of an object in java, instead of a pointer

First, have your class implement the Cloneable interface. Without this, calling clone() on your object will throw an exception.

Next, override Object.clone() so it returns your specific type of object. The implementation can simply be:

@Override
public MyObject clone() {
return (MyObject)super.clone();
}

unless you need something more intricate done. Make sure you call super.clone(), though.

This will call all the way up the hierarchy to Object.clone(), which copies each piece of data in your object to the new one that it constructs. References are copied, not cloned, so if you want a deep copy (clones of objects referenced by your object), you'll need to do some extra work in your overridden clone() function.

Duplicating objects in Java

Every variable in Java is a reference. So when you do

SomeClass s2 = s1;

you just point s2 to the same object as s1 points to. You are actually assigning the value of the reference s1 (which points to an instance of SomeClass) to s2. If you modify s1, s2 will be modified as well (because it points to the same object).

There is an exception, primitive types: int, double, float, boolean, char, byte, short, long. They are stored by value. So when using =, you only assign the value, but they can not point to the same object (because they are not references). This means that

int b = a;

only sets the value of b to the value of a. If you change a, b will not change.

At the end of the day, everything is assignment by value, it's just the value of the reference and not the value of the object (with the exception of primitive types as mentioned above).

So in your case, if you want to make a copy of s1, you can do it like this:

SomeClass s1 = new SomeClass("first");
SomeClass s2 = new SomeClass(s1.getText());

Alternatively, you can add a copy constructor to SomeClass that takes an instance as argument and copies it into its own instance.

class SomeClass {
private String text;
// all your fields and methods go here

public SomeClass(SomeClass copyInstance) {
this.text = new String(copyInstance.text);
}
}

With this you can copy an object pretty easily:

SomeClass s2 = new SomeClass(s1);

Copy a model object in java

You can also use Dozer library: http://dozer.sourceforge.net/

You can easily copy whole POJOs from one to another like this:

Mapper mapper = new DozerBeanMapper();
DestinationObject destObject = mapper.map(sourceObject, DestinationObject.class);

What is really usufull you can configure your own mappings like this:

<mapping>
<class-a>yourpackage.yourSourceClassName</class-a>
<class-b>yourpackage.yourDestinationClassName</class-b>
<field>
<a>yourSourceFieldName</a>
<b>yourDestinationFieldName</b>
</field>
</mapping>

Additionally you can use different Technics to copy object, use custom factories, special getters/setters and a lot useful things: http://dozer.sourceforge.net/documentation/mappings.html



Related Topics



Leave a reply



Submit