vico
vico

Reputation: 18221

Copying object fields

What is the best way of copying classes when I need to have two independent variables? I have simple class:

public class MyClass 
{
boolean a = false;
String b= "not empty";
}

do I need make some method like :

assign(MyClass data )
{
a= data.a;
b= data.b;
}

Is there any automatic method to copy (duplicate) objects in java?

Upvotes: 1

Views: 105

Answers (3)

Ahmed Adel Ismail
Ahmed Adel Ismail

Reputation: 2184

these are 2 approaches, first using the Cloneable interface (which is not preferred), and the other is creating a method inside your class that returns a new instance with the current state

public class MyObject implements Cloneable {

    private int num = 0;

    // a method to show how to clone an object
    private void copyObject() throws CloneNotSupportedException {

        MyObject a = new MyObject();
        a.num = 10;

        MyObject b = (MyObject)a.clone();
        System.out.println(b.num);
        // result : 10
        // values are copied to the new object

        b.num = 20;
        System.out.println(a.num + " - " + b.num);
        // result : 10 - 20
        // 2 different objects, the new object didnt effect the original one
    }

    // a method to create a copy
    public MyObject copy() {
        MyObject copy = new MyObject();
        copy.num = this.num;
        // do the same to all your variables
        return copy;
    }

    // a method to show how to use the copy() method
    private void useCopy(){

        MyObject a = new MyObject();
        a.num = 100;

        MyObject b = a.copy();

        System.out.println(a.num+" - "+b.num); 
        //  result : 100 - 100
        //  value copied to the new Object

        a.num = 10;
        b.num = 20;
        System.out.println(a.num + " - " + b.num);
        //  result : 10 - 20
        //  although b was copied from a, but both are 2 different Objects
    }
}

there are also another ways to copy values using reflection package, but after all, are you sure you are on the right track concerning design ?

if you can tell us what you are thinking of, maybe a design pattern may help

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213371

do I need make some method like :

Pretty close. Instead of making it method, you should make it a constructor. Such constructors are called copy constructor, and you create them like this:

MyClass(MyClass data) {  
    a = data.a;
    b = data.b;
}

And then to create copy of instance, you use the constructor like this:

MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass(obj1);

Copy constructor can be tedious:

Using copy constructor to create deep-copy can be tedious, when your class has mutable fields. In which case, assignment like those will just create a copy of the reference, and not the object itself. You have to create copy of those fields also (If you want a deep copy). This can go recursive.

A better way to create deep copy is to Serialize and then Deserialize your object.

Why not use clone()?

Addition of clone() method in Object class was a big mistake, IMO. You should avoid using it for cloning objects.

  • For one, to use that method, you have to implement Cloneable interface, which by surprise doesn't actually have that clone() method. In fact, it's just a marker interface.
  • Return type of Object#clone() method is Object. So, this means that you can actually return an instance of a completely unrelated class, thus leading your code to a potential ClassCastException at runtime.

See also:

Upvotes: 3

codeMan
codeMan

Reputation: 5758

You can manually do a deep-clone using a copy constructor or u can use a utility available for that purpose, such as this one: https://code.google.com/p/cloning/

If ur class has many non-primitive member variables the complexity of deepcloning increases, I suggest you use that utility.

Upvotes: 1

Related Questions