1

This might have been asked already but since I am not so sure how to phrase it I could not find it.

Essentially

Suppose we have the class a, (see code below), and we want to copy an instance of it, a1, to another instance a2.

So, in my main I would have a1.copy(a2) I know that using copy2 method this will work. However copy1 will not. I just would like to clarify why this is. Is it because the parameter is just a "copy" of the object, so the object itself (a2) is not altered.

class a {
    private int val;
    public class(int val){
        this.val = val; 
    }

    public void copy1(a obj){
        obj = this;
    }

    public void copy2(a obj) {
            obj.val = this.val;
    }
}
Andrew Stubbs
  • 4,322
  • 3
  • 29
  • 48
NaC114
  • 83
  • 8

4 Answers4

1

When you call the method copy1(), the parameter is a copy of the reference of the object a obj. When you set it to this (obj = this;) the copy of the reference of the object is replaced by the current object, but the original object and the reference to the object at the place you are calling the copy1() method stays the same. You just change the copy of the reference of the object.

When you call the method copy2(), no reference work is done there, and you are doing one by one matching of the variables (properties of the objects) and there is no copy of reference work there. You are changing the object itself. In this case, this.val = obj.val; would also work.

For more understanding, check this topic and check how other languages like C or C++ handle parameter passing issue.

Community
  • 1
  • 1
Seyf
  • 889
  • 8
  • 16
1

I think you might want to look into how parameters are passed in Java.

For primitive variables, they are passed by value. If I recall correctly, this means that a temporary value is passed and any changes to the variable in the function update only the temporary variable.

However, objects are passed by reference. This means that instead of the entire object, essentially a pointer to where the object is in memory is passed.

What this means is that updating an object's variables produces lasting effects on that object. Additionally, if you set one object equal to another object, you're really just setting the memory references to be the same (a byproduct of this is that the values of the variables in the objects are then linked).

This explains why the first function fails to do what you want (you're setting the memory locations to be the same). And the second succeeds (you're performing a deep copy into a new memory address).

HedonicHedgehog
  • 592
  • 1
  • 6
  • 17
1

The key to understand your problem is that Java's method call is always pass-by-value, not by reference.

When you call a.copy1(b), Java copies a value of b's reference(say it is called b_copy, please note that b_copy points to the same memory location as b), and then pass b_copy to the method copy1.

And in your method of copy1, Java only changes the reference of b_copy.

 public void copy1(b_copy){
    b_copy=a;
 }

So now :

b_copy: b_copy=a;
b:      b does not change at all;

When the method ends,b_copy dies. So nothing changes on b!

While a.copy2(b) manipulates on the object itself, but it still copies a new value of b(say b_copy again) and pass into copy2

 public void copy2(b_copy){
     b_copy.val = a.val;
 }

Since b_copy points to the same memory of b, so when you do changes on b_copy.val, you also does the same on b.val itself. That's the reason why b changes.

So now :

b_copy: b_copy, but b_copy's val changes
b:      b's val also changes, since b_copy is points to the b's memory location;

And then when method ends, b_copy dies, and b has changed!

You may find more discussion on Is Java "pass-by-reference" or "pass-by-value"?

And you may also need How do I copy an object in Java?

Community
  • 1
  • 1
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
0

Your reasoning is correct. When you pass an object to a method (as you do in copy1), all you are doing is creating another reference to the same object. If you override this reference, as copy1 does, it just means that that reference now points to a different object - it does not, and can not, change the original object.

Mureinik
  • 297,002
  • 52
  • 306
  • 350