How do I copy an object in Java?
Asked Answered
T

23

926

Consider the code below:

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 change dum without affecting the dumtwo. But the code above 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?

Typography answered 15/5, 2009 at 14:30 Comment(0)
F
704

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.

Flatling answered 15/5, 2009 at 14:35 Comment(16)
But then he'd have to change his code to DummyBean two = new DummyBean(one); Right?Cenobite
Does this method effectively accomplish the same thing as a deep copy?Stuppy
@MatthewPiziak, to me - this would not be a deep clone since any nested objects would still be referencing the original source instance, not a duplicate unless each reference (non-value type) object supplies the same constructor template as above.Juarez
Can someone confirm if this really works? It looks like it will create another DummyBeam instance, but both dummy's will still refer to the same String...Wetterhorn
@Timmmm: Yes, they will reference the same String but because it is immutable, it is ok. Same goes for primitives. For non-primitives, you would just do copy contructor call recursively. e.g. If DummyBean referenced FooBar then FooBar should have contructor FooBar(FooBar another), and dummy should call this.foobar = new FooBar(another.foobar)Flatling
If you do: DummyBean two = new DummyBean(one); one.setDummy("johndoe"); then two.getDummy() will be "johndoe", so this is not a correct answer.Bereniceberenson
@ChristianVielma: No, it won't be "johndoe". Like Timmmm said, the string itself is immutable. With one, setDummy(..) you set the reference in one to point to "johndoe", but not the one in one.Enyo
This solution does not work on abstract base classes, which makes it pretty useless in many OOP design patterns.Nobe
With all Eclipse code generation features, I am surprised there isn't one to create the constructor. I sometimes have configuration classes with a lot of fields and it sure would help if Eclipse could generate that copy constructor automatically.Lacustrine
This is a very limited solution, because it only works when there are no subclasses. That is, if you know at compile time what class the object has.Helmer
I really don't like the copy constructor since this is not clear that a constructor would copy an instance instead of simply take some values or using his reference. I prefer to use a clone method that is more comprehensive. With DummyBean DummyBean.clone(), you can tell easily the output but the constructor is vague.Vie
It could be good to narrow the description as "Every object has also a protected clone method which can be used to copy the object by implementing Cloneable interface".Contango
if you have 100 fields, this is hard to do.Oblige
How to copy all properties of a class at once rather than doing the assignment one by one. Because you see i have a class with around 50 properties, and there is a high chance that i might miss assignment of some of them. Any way out ?Ranna
What about classes that have already been defined and can't be changed?Dangle
How to create it automatically?Confederation
R
457

Basic: Object Copying in Java.

Let us Assume an object- obj1, that contains two objects, containedObj1 and containedObj2.
enter image description here

shallow copying:
shallow copying creates a new instance of the same class and copies all the fields to the new instance and returns it. Object class provides a clone method and provides support for the shallow copying.
enter image description here

Deep copying:
A deep copy occurs when an object is copied along with the objects to which it refers. Below image shows obj1 after a deep copy has been performed on it. Not only has obj1 been copied, but the objects contained within it have been copied as well. We can use Java Object Serialization to make a deep copy. Unfortunately, this approach has some problems too(detailed examples).
enter image description here

Possible Problems:
clone is tricky to implement correctly.
It's better to use Defensive copying, copy constructors(as @egaga reply) or static factory methods.

  1. If you have an object, that you know has a public clone() method, but you don’t know the type of the object at compile time, then you have problem. Java has an interface called Cloneable. In practice, we should implement this interface if we want to make an object Cloneable. Object.clone is protected, so we must override it with a public method in order for it to be accessible.
  2. Another problem arises when we try deep copying of a complex object. Assume that the clone() method of all member object variables also does deep copy, this is too risky of an assumption. You must control the code in all classes.

For example org.apache.commons.lang.SerializationUtils will have method for Deep clone using serialization(Source). If we need to clone Bean then there are couple of utility methods in org.apache.commons.beanutils (Source).

  • cloneBean will Clone a bean based on the available property getters and setters, even if the bean class itself does not implement Cloneable.
  • copyProperties will Copy property values from the origin bean to the destination bean for all cases where the property names are the same.
Retiring answered 23/3, 2012 at 5:47 Comment(5)
Can you please explain what is object contained within another?Bartley
@Chandra Sekhar "shallow copying creates a new instance of the same class and copies all the fields to the new instance and returns it" that's wrong to mention all the fields,bcz objects don't get copied only the references get copied which points to the same object that old one(original) was pointing to.Articulator
@sunny - Chandra's description is correct. And so is your description of what happens; I am saying that you have an incorrect understanding of the meaning of "copies all the fields". The field is the reference, it is not the object being referred to. "copying all fields" means "copying all those references". It is good that you pointed out what exactly this means, for anyone else who has the same mis-interpretation as you, of the statement "copying all fields". :)Pyrolysis
... if we think in terms of some lower-level OO language, with "pointers" to objects, such a field would contain the address in memory (such as "0x70FF1234") at which the object data is found. That address is the "field value" that is being copied (assigned). You are correct that the end result is that both objects have fields that refer to (point at) the same object.Pyrolysis
baeldung.com/java-deep-copyDockyard
T
166

In the package import org.apache.commons.lang.SerializationUtils; there is a method:

SerializationUtils.clone(Object);

Example:

this.myObjectCloned = SerializationUtils.clone(this.object);
Turbine answered 2/5, 2013 at 19:45 Comment(6)
As long as the object implements SerializablePerplex
In this case cloned object has no reference to the original, if the last one is static.Telegonus
A third party library just to clone object!Gualtiero
@Khan, "a third party library just to" is an entirely separate discussion! :DMoose
I get java.lang.NoClassDefFoundError on Android 4, 5 and 6: Fatal Exception: java.lang.NoClassDefFoundError org.apache.commons.lang3.-$$Lambda$Validate$0cAgQbsjQIo0VHKh79UWkAcDRWkSkeg
This works only if all the objects inside this Object are serialisable.Dikmen
D
113

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 wherever you want to get another object, simple perform cloning. e.g:

Deletable del = new Deletable();
Deletable delTemp = (Deletable ) del.clone(); // this line will return you an independent
                                 // object, the changes made to this object will
                                 // not be reflected to other object
Deeann answered 17/5, 2010 at 9:27 Comment(7)
Did you test this? I could use this for my project and it is important to be correct.Heriberto
@Heriberto I've tested it. Works perfectly on my production appDarbydarce
After cloning, when you modify the original object, it is modifying the clone as well.Showker
I also have the same problem, when I change the properties of the original object then the values are changed in the cloned object.Cassaundracassava
This is wrong in that it is not a deep copy which was asked for.Pard
This method clone the pointer which points for the cloneable object, but all the properties inside both objects are the same, So there is a new object created in the memory, but the data inside each object is the same data from memoryIncubation
To Bhasker Tiwari's defense however, the example given concerns a String reference only, which is perfectly safe (and infact preferred) to share among copies. Admittedly it should have been made clear that this is a special case and that further action needs to be taken when deep cloning objects with references to mutable objects.Gherardi
V
47

Why is there no answer for using Reflection API?

private static Object cloneObject(Object obj){
        try{
            Object clone = obj.getClass().newInstance();
            for (Field field : obj.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                field.set(clone, field.get(obj));
            }
            return clone;
        }catch(Exception e){
            return null;
        }
    }

It's really simple.

EDIT: Include child object via recursion

private static Object cloneObject(Object obj){
        try{
            Object clone = obj.getClass().newInstance();
            for (Field field : obj.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                if(field.get(obj) == null || Modifier.isFinal(field.getModifiers())){
                    continue;
                }
                if(field.getType().isPrimitive() || field.getType().equals(String.class)
                        || field.getType().getSuperclass().equals(Number.class)
                        || field.getType().equals(Boolean.class)){
                    field.set(clone, field.get(obj));
                }else{
                    Object childObj = field.get(obj);
                    if(childObj == obj){
                        field.set(clone, clone);
                    }else{
                        field.set(clone, cloneObject(field.get(obj)));
                    }
                }
            }
            return clone;
        }catch(Exception e){
            return null;
        }
    }
Vintner answered 16/8, 2014 at 9:27 Comment(9)
This looks much better, but you only need to consider final fields as setAccessible(true) might fail, so maybe u need to separately handle the exception IllegalAccessException thrown when calling field.set(clone, field.get(obj)) separately.Nobe
I liked it so much but can you refactor it to use generics ? private static <T> T cloneObject(T obj) { .... }Viviparous
I think it's issue when we have reference from properties to it parents: Class A { B child; } Class B{ A parent; }Cherimoya
It fails even int this situation, need to be handled, i will play with it tomorow. class car { car car = new car(); }Disjuncture
This is not cloning the property with their values. Always returning a cloned object with all properties set to nullLuis
worked for me. i would change line field.getType().getSuperclass().equals(Number.class) to (field.getType().getSuperclass() != null && field.getType().getSuperclass().equals(Number.class)) to avoid NPE when field type has no superclassSarah
I had some issues related to null values using getSuperClass, which can be solved using Number.class.equals(field.getType().getSuperclass()) and with arrays it didn't worked, which can be fixed like this if(Object[].class.isAssignableFrom(field.getType())){ Object[] values = ((Object[])field.get(obj)); Object[] valuesClone = Arrays.copyOf(values, values.length); field.set(clone, valuesClone); }Impetuosity
This is error prone. Not sure how will it handle collectionsOblige
@WillingLearner, this creates a shallow copy, right? And what about fields inherited from super classes? I assume you would want to recurse on getSuperClass() to make sure these fields are copied too?Gherardi
H
34

I use Google's JSON library to serialize it then create a new instance of the serialized object. It does deep copy with a few restrictions:

  • there can't be any recursive references

  • it won't copy arrays of disparate types

  • arrays and lists should be typed or it won't find the class to instantiate

  • you may need to encapsulate strings in a class you declare yourself

I also use this class to save user preferences, windows and whatnot to be reloaded at runtime. It is very easy to use and effective.

import com.google.gson.*;

public class SerialUtils {

//___________________________________________________________________________________

public static String serializeObject(Object o) {
    Gson gson = new Gson();
    String serializedObject = gson.toJson(o);
    return serializedObject;
}
//___________________________________________________________________________________

public static Object unserializeObject(String s, Object o){
    Gson gson = new Gson();
    Object object = gson.fromJson(s, o.getClass());
    return object;
}
       //___________________________________________________________________________________
public static Object cloneObject(Object o){
    String s = serializeObject(o);
    Object object = unserializeObject(s,o);
    return object;
}
}
Humidistat answered 9/7, 2013 at 20:14 Comment(1)
This works great. But watch out if you try to clone something like List<Integer>. It will be buggy, my Integers got turned into Doubles, 100.0. It took me a long while to understand why are they like that. The solution was to clone Integers them one by one and add to the list in a cycle.Stopper
U
24

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

Check out this wiki article about copying objects.

Refer here: Object copying

Uraemia answered 15/5, 2009 at 14:36 Comment(0)
A
18

Add Cloneable and below code to your class

public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

Use this clonedObject = (YourClass) yourClassObject.clone();

Accused answered 31/7, 2013 at 15:0 Comment(0)
A
18

Deep Cloning is your answer, which requires implementing the Cloneable interface and overriding the clone() method.

public class DummyBean implements Cloneable {

   private String dummy;

   public void setDummy(String dummy) {
      this.dummy = dummy;
   }

   public String getDummy() {
      return dummy;
   }

   @Override
   public Object clone() throws CloneNotSupportedException {
      DummyBean cloned = (DummyBean)super.clone();
      cloned.setDummy(cloned.getDummy());
      // the above is applicable in case of primitive member types like String 
      // however, in case of non primitive types
      // cloned.setNonPrimitiveType(cloned.getNonPrimitiveType().clone());
      return cloned;
   }
}

You will call it like this DummyBean dumtwo = dum.clone();

Aswarm answered 8/7, 2014 at 17:14 Comment(1)
dummy, a String, is immutable, you don't need to copy itSibby
M
13

This works too. Assuming model

class UserAccount{
   public int id;
   public String name;
}

First add compile 'com.google.code.gson:gson:2.8.1' to your app>gradle & sync. Then

Gson gson = new Gson();
updateUser = gson.fromJson(gson.toJson(mUser),UserAccount.class);

You can exclude using a field by using transient keyword after access modifier.

Note: This is bad practice. Also don't recommend to use Cloneable or JavaSerialization It's slow and broken. Write copy constructor for best performance ref.

Something like

class UserAccount{
        public int id;
        public String name;
        //empty constructor
        public UserAccount(){}
        //parameterize constructor
        public UserAccount(int id, String name) {
            this.id = id;
            this.name = name;
        }

        //copy constructor
        public UserAccount(UserAccount in){
            this(in.id,in.name);
        }
    }

Test stats of 90000 iteration:
Line UserAccount clone = gson.fromJson(gson.toJson(aO), UserAccount.class); takes 808ms

Line UserAccount clone = new UserAccount(aO); takes less than 1ms

Conclusion: Use gson if your boss is crazy and you prefer speed. Use second copy constructor if you prefer quality.

You can also use copy constructor code generator plugin in Android Studio.

Myke answered 26/1, 2017 at 8:50 Comment(2)
Why did you suggest it if it's bad practice?Quevedo
Thanks @ParthMehrotra now improvedMyke
S
10

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

Here: clone (Java method)

Servility answered 15/5, 2009 at 14:35 Comment(0)
I
10

Use a deep cloning utility:

SomeObjectType copy = new Cloner().deepClone(someObject);

This will deep copy any java object, check it out at https://github.com/kostaskougios/cloning

Irisation answered 23/10, 2011 at 16:12 Comment(1)
didn't work for me using a custom class. getting the following exception: java.lang.NoClassDefFoundError: sun.reflect.ReflectionFactoryBasil
S
10

Alternative to egaga's constructor method of copy. You probably already have a POJO, so just add another method copy() which returns a copy of the initialized object.

class DummyBean {
    private String dummyStr;
    private int dummyInt;

    public DummyBean(String dummyStr, int dummyInt) {
        this.dummyStr = dummyStr;
        this.dummyInt = dummyInt;
    }

    public DummyBean copy() {
        return new DummyBean(dummyStr, dummyInt);
    }

    //... Getters & Setters
}

If you already have a DummyBean and want a copy:

DummyBean bean1 = new DummyBean("peet", 2);
DummyBean bean2 = bean1.copy(); // <-- Create copy of bean1 

System.out.println("bean1: " + bean1.getDummyStr() + " " + bean1.getDummyInt());
System.out.println("bean2: " + bean2.getDummyStr() + " " + bean2.getDummyInt());

//Change bean1
bean1.setDummyStr("koos");
bean1.setDummyInt(88);

System.out.println("bean1: " + bean1.getDummyStr() + " " + bean1.getDummyInt());
System.out.println("bean2: " + bean2.getDummyStr() + " " + bean2.getDummyInt());

Output:

bean1: peet 2
bean2: peet 2

bean1: koos 88
bean2: peet 2

But both works well, it is ultimately up to you...

Sox answered 19/9, 2019 at 8:29 Comment(0)
P
8

Use gson for duplicating an object.

public static <T>T copyObject(Object object){
    Gson gson = new Gson();
    JsonObject jsonObject = gson.toJsonTree(object).getAsJsonObject();
    return gson.fromJson(jsonObject,(Type) object.getClass());
}

Assume I have an object person.So

Person copyPerson = copyObject(person);

Note: The performance is much slower.

Pleurodynia answered 14/5, 2020 at 17:16 Comment(1)
Thats a Good idea!Sizemore
Y
7

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 advice on different techniques for accomplishing a copy.

Yourself answered 15/5, 2009 at 14:33 Comment(0)
N
7

Pass the object that you want to copy and get the object you want:

private Object copyObject(Object objSource) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(objSource);
            oos.flush();
            oos.close();
            bos.close();
            byte[] byteData = bos.toByteArray();
            ByteArrayInputStream bais = new ByteArrayInputStream(byteData);
            try {
                objDest = new ObjectInputStream(bais).readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return objDest;

    }

Now parse the objDest to desired object.

Happy Coding!

Nessa answered 23/12, 2014 at 12:46 Comment(1)
A good solution to have a solution. But if the purpose of cloning is to improve performance, this solution is not working because it has a heavy processesingLust
S
6

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.

Soble answered 15/5, 2009 at 14:43 Comment(0)
L
4
class DB {
  private String dummy;

  public DB(DB one) {
    this.dummy = one.dummy; 
  }
}
Landri answered 2/3, 2014 at 13:35 Comment(0)
M
3

You can deep copy automatically with XStream, from http://x-stream.github.io/:

XStream is a simple library to serialize objects to XML and back again.

Add it to your project (if using maven)

<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.3.1</version>                
</dependency>

Then

DummyBean dum = new DummyBean();
dum.setDummy("foo");
DummyBean dumCopy = (DummyBean) XSTREAM.fromXML(XSTREAM.toXML(dum));

With this you have a copy without the need to implement any cloning interface.

Minaret answered 25/7, 2011 at 16:59 Comment(4)
Converting to/from XML doesn't seem very ... elegant. To put it mildly!Wetterhorn
Take a look to java.beans.XMLEncoder for a standard Java API that serializes to XML too (although not precisely for deep copy purposes).Minaret
do you realize how heavy this is ?Stormie
Way to much overhead in my opinion, since you need to add a 3rd party library and do object serialization which most likely has a huge performance impact.Fetid
L
3
public class MyClass implements Cloneable {

private boolean myField= false;
// and other fields or objects

public MyClass (){}

@Override
public MyClass clone() throws CloneNotSupportedException {
   try
   {
       MyClass clonedMyClass = (MyClass)super.clone();
       // if you have custom object, then you need create a new one in here
       return clonedMyClass ;
   } catch (CloneNotSupportedException e) {
       e.printStackTrace();
       return new MyClass();
   }

  }
}

and in your code:

MyClass myClass = new MyClass();
// do some work with this object
MyClass clonedMyClass = myClass.clone();
Lander answered 4/2, 2018 at 8:27 Comment(1)
There is no point in set "throws CloneNotSupportedException" in the declaration if you try catch the exception and is not thrown up. So, you can just remove it.Pingpingpong
A
1

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.

Adria answered 15/5, 2009 at 19:49 Comment(0)
K
1

If you can add an annotation to the source file, an annotation processor or code generator like this one can be used.

import net.zerobuilder.BeanBuilder

@BeanBuilder
public class DummyBean { 
  // bean stuff
}

A class DummyBeanBuilders will be generates, which has a static method dummyBeanUpdater to create shallow copies, the same way as you would do it manually.

DummyBean bean = new DummyBean();
// Call some setters ...
// Now make a copy
DummyBean copy = DummyBeanBuilders.dummyBeanUpdater(bean).done();
Katekatee answered 5/2, 2017 at 16:17 Comment(0)
E
1

Using Kotlin extension function

fun <T : Any?> T.duplicate(): T? {
    var copyObject: T? = null
    try {
        val byteArrayOutputStream = ByteArrayOutputStream()
        val objectOutputStream = ObjectOutputStream(byteArrayOutputStream)
        objectOutputStream.writeObject(this)
        objectOutputStream.flush()
        objectOutputStream.close()
        byteArrayOutputStream.close()
        val byteData = byteArrayOutputStream.toByteArray()
        val byteArrayInputStream = ByteArrayInputStream(byteData)
        try {
            copyObject = ObjectInputStream(byteArrayInputStream).readObject() as T
        } catch (e: ClassNotFoundException) {
            e.printStackTrace()
        }
    } catch (e: IOException) {
        e.printStackTrace()
    }
    return copyObject
}

Use case

var object = Any()
var duplicateObject = object.duplicate()

Java

<T extends Object> T copyObject(T sourceObject) {

    T copyObject = null;

    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(sourceObject);
        objectOutputStream.flush();
        objectOutputStream.close();
        byteArrayOutputStream.close();
        byte[] byteData = byteArrayOutputStream.toByteArray();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteData);
        try {
            copyObject = (T) new ObjectInputStream(byteArrayInputStream).readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return copyObject;
}

Use case

Object object = new Object();
Object duplicateObject = copyObject(object);

==============================================

Kotlin Update

If you use data class then you will have copy method that copies the Kotlin data class. Cool thing is you could also pass some values to modify the object with new copy. I would recommend this way.

Example:

//class

data class TestModel(val title: String, var subtitle: String)

Use case

val testClass = TestModel("Test title", "Test subtitle")

val newInstance = testClass.copy(subtitle = "new subtitle for copy instance")
Echevarria answered 15/6, 2021 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.