Undefined
Undefined

Reputation: 1929

Creating a copy of an object without coping it's property references

To explain:

public class SomeClass {
  int aNumber = 0;

  public void changeNumber(int number){
    aNumber = number;
  }

  public int getNumber(){
    return aNumber;
  }
} 


public class Testapp {

  public static void main(String[] args) {
    NewClass object1 = new NewClass();
    NewClass object2 = object1;

    object1.changeNumber(5);
    object2.changeNumber(2);
    System.out.println("object1: "+object1.getNumber());
    System.out.println("object2: "+object2.getNumber());
  }
}

This will output: object1: 2 object2: 2

How would I make it output: object1: 5 object2: 2

Upvotes: 0

Views: 9055

Answers (6)

Code-Apprentice
Code-Apprentice

Reputation: 83517

You have two options:

1) Implement a copy constructor:

public SomeClass(SomeClass c) {
  this.aNumber = c.aNumber;
}

And then to create a copy:

SomeClass object2 = new SomeClass(object1);

2) Implement the Cloneable interface and override Object.clone():

public class SomeClass implements Cloneable {
  public SomeClass clone() {
    super.clone();
  }
}

Then to create a copy:

SomeClass object2 = object1.clone();

Note: I have omitted any code that does not change.

Upvotes: 6

Rutesh Makhijani
Rutesh Makhijani

Reputation: 17225

Object is an instance of class and capable of holding state. Object is created using new operator - in your case object is created at new NewClass()

NewClass object1 and NewClass object2 are object reference variable - these are like pointers in C/C++. Reference variable do not hold state - they just point to memory location where object state is located.

When you say NewClass object1 = new NewClass() - the new operator creates instance of NewClass and returns the memory location that gets stored in object1 reference variable. When you say NewClass object2 = object1 - the memory location details held in object1 are copied to object2 - important thing to note over here is that no new object is created. There is only one object and hence only one state - that is why when you change state using object1 or object2 reference variable the previous value is overwritten.

If you want two objects then you need to invoke new twice - that will allocate separate memory location for each object. In case you have an object and want similar copy of the object - copying its state to new memory location - then you need to use the clone() method of Object class. Cloning creates a new object and copies the current state of object to new object - so if you call NewClass object2 = object1.clone() that would create new object and copy state of object1 to that, returning the reference to object2 - here object2 is also called as local copy. You need to follow java specifications for cloning - that is you need to implement Cloneable interface to support cloning of objects - please refer this link to start with http://en.wikipedia.org/wiki/Clone_(Java_method)

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

1. You have copied the reference Not the object.

2. As both the Object Reference Variables are pointing to the Same object on the heap, you are getting 2 as the answer for both of them..

3. Use new operator to create and assign another object to the object2 reference.

One more thing... its not NewClass, but SomeClass as its mentioned in your code

SomeClass object1 = new SomeClass();
SomeClass object2 = new SomeClass();
object1.changeNumber(5);
object2.changeNumber(2);
System.out.println("object1: "+object1.getNumber());
System.out.println("object2: "+object2.getNumber());

Upvotes: 0

jrad
jrad

Reputation: 3190

How you have it set up right now is that both object1 and object2 are pointing at the same spot in memory. You will have to do this:

NewClass object1 = new NewClass();
NewClass object2 = new NewClass();
object1.changeNumber(5);
object2.changeNumber(2);
System.out.println("object1: "+object1.getNumber());
System.out.println("object2: "+object2.getNumber());

This way, object1 and object2 are completely separate Objects.

Upvotes: 1

Robert
Robert

Reputation: 4406

 public class Testapp {
public static void main(String[] args) {
 NewClass object1 = new NewClass();
 NewClass object2 = new NewClass();
  object1.changeNumber(5);
 object2.changeNumber(2);
 System.out.println("object1: "+object1.getNumber());
 System.out.println("object2: "+object2.getNumber());   } } 

You should call a new instantiation of the object otherwise they refer to the same position in memory.

Upvotes: 0

SJuan76
SJuan76

Reputation: 24780

You are not doing a copy of the object, you are copying the reference. That means that both variables refer to the same object.

To create an object, you must use new or call a method that uses it.

Since you want a copy, you should make your object implement Cloneable (and, if needed, implement clone()).

Upvotes: 1

Related Questions