What it means is, given
class Foo {
private int bar;
public void setBar(int value) {
bar = value;
}
public int getBar() {
return bar;
}
}
And
public void doSomethingToFoo(Foo foo) {
foo.setBar(42);
}
The change to bar
would be visible at the callsite. This is the manipulation that sticks. The reference to the original object was passed in. The setter was invoked for that object, so the getter at the callsite would return the new value of bar.
Foo foo = new Foo();
doSomethingToFoo(foo);
int bar = foo.getBar(); // gets 42
However, given
public void doSomethingElseWithFoo(Foo foo) {
foo = new Foo(); // assigning new instance to variable
foo.setBar(117);
}
This change would not be visible at the callsite, as the foo
variable inside the method has been reassigned. The reference was merely passed by value, the variables are not actually linked or connected in any way, they simply had a value (the reference) in common. We have now overwritten the reference that one variable had inside the method, the variable at the callsite has the same reference it had before.
Foo foo = new Foo();
foo.setBar(7);
doSomethingElseWithFoo(foo);
int bar = foo.getBar(); // still is 7!
Does that make sense?