6

Is there a generic way to achieve copying an existing object into another?

Assume MyObj has an id and name fields. Like this:

MyObj myObj_1 = new MyObj(1, "Name 1");
MyObj myObj_2 = new MyObj(2, "Name 2");

Instead of

myObj_2.setName(myObj_1.getName()) // etc for each field

do something as following:

myObj_2.copyFrom(myObj_1)

so that they are different instances, but have equal properties.

P.T.
  • 24,557
  • 7
  • 64
  • 95
EugeneP
  • 11,783
  • 32
  • 96
  • 142
  • 1
    see http://stackoverflow.com/questions/64036/how-do-you-make-a-deep-copy-of-an-object – Gad Mar 29 '10 at 09:27
  • 1
    This [Answer](http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java/9834683#9834683) will help you. – Chandra Sekhar Mar 23 '12 at 07:42

6 Answers6

8

The convention is to do this at construction time with a constructor that takes one parameter of its own type.

MyObj myObj_2 = new MyObj(myObj_1);

There is no Java convention to overwrite the existing properties of an object from another. This tends to go against the preference for immutable objects in Java (where properties are set at construction time unless there is a good reason not to).

Edit: regarding clone(), many engineers discourage this in modern Java because it has outdated syntax and other drawbacks. http://www.javapractices.com/topic/TopicAction.do?Id=71

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
7

Use copy constructor:

public class YourObject {
  private String name;
  private int age;

  public YourObject(YourObject other) {
     this.name = other.name;
     this.age = other.age;
  }
}
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
4

Object.clone() & interface Cloneable

Maxime ARNSTAMM
  • 5,274
  • 10
  • 53
  • 76
  • 4
    Although this is correct, Joshua Bloch warns against using clone in Effective Java. Better to create a copy constructor. – Steve McLeod Mar 29 '10 at 10:54
0

The clone()-method is for exactly this job.

Dishayloo
  • 390
  • 2
  • 8
0

You can use introspection to automate the implementation of your clone routines, so you can be safe you do not forget to copy some fields.

codymanix
  • 28,510
  • 21
  • 92
  • 151
0

The clone() method is best suited for these requirements. Whenever the clone() method is called on an object, the JVM will actually create a new object and copy all the content of previous object into the newly created object. Before using the clone() method you have to implement the Cloneable interface and override the clone() method.

public class CloneExample implements Cloneable
{
    int id;
    String name;

    CloneExample(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public static void main(String[] args) {
        CloneExample obj1 = new CloneExample(1,"Name_1");

        try {
            CloneExample obj2 = (CloneExample) obj1.clone();
            System.out.println(obj2.id);
            System.out.println(obj2.name);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
mange
  • 3,172
  • 18
  • 27