I'd like to create a class that stores some events in the form of methods. When one of them is called i'd like to make it to take an object and to modify it, but if i'm not wrong the parameter of that method would be only a copy of the real object, so it wouldn't change anything about it. Is there a way to do it? I hope i have been clear enough...
-
1That isn't the case. Java passes object by "passing a reference by value". This is close to passing by reference and the object you are working with **is the same object** – Richard Tingle Feb 16 '16 at 22:11
-
Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Richard Tingle Feb 16 '16 at 22:13
-
You are wrong. Java is pass by value, so read about that first. Also, setters might be the word you are looking for. – John Feb 16 '16 at 22:13
-
1@John java is indeed always pass by value. But it passes **a reference** by value. Saying it's pass by value is technically correct but deeply misleading – Richard Tingle Feb 16 '16 at 22:15
-
Yes i am aware of that. Passing by reference would imply that method can change to which object reference 'points' at the caller's site. – John Feb 16 '16 at 22:20
-
@John indeed passing a reference by value is closer to pass by reference than it is to pass an object by value. But they are still different. As you say changing the variable to point to a different objects doesn't affect the calling variable. – Richard Tingle Feb 16 '16 at 22:45
1 Answers
Yes, you are right by saying that JAVA is pass by value
and it will send a copy of that object but remember that in case of an object that copy will point to the same references as that of the original object.
Whatever you'll modify in the passed object will also get reflected in the original object. This is because the references are still same.
For Example,
public class Foo {
private int x;
/* Getter-Setter */
}
Now, consider following two objects:
Foo realObject = new Foo();
realObject.setX(1);
/* Call someMethod */
someMethod(Foo realObject);
/* someMethod Implementation */
public void someMethod(Foo copyObject) {
copyObject.setX(5);
}
Now, the changes you did in copyObject
will be reflected in realObject
because internally it references the same location. Hence, the value of x
in realObject
will change from 1
to 5
.
The best way to achieve what you are trying to do is by using the copy constructor in your POJO class.
For Example,
public class Foo {
private int x;
public Foo(Foo f) {
this.x = f.x;
}
}
Now, you can copy the object as follows:
Foo realObject = new Foo();
realObject.setX(1);
/* Make Copy */
Foo copyObject = new Foo(realObject);
copyObject.setX(5);
This time changes to copyObject
will not impact realObject
. The value of x
in realObject
will still be 1
.

- 9,548
- 5
- 37
- 54
-
Pardon? Are you saying the object is a copy or the reference is a copy? I'm also not sure why you'd need a copy constructor – Richard Tingle Feb 16 '16 at 22:14