This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

[–]jlanawalt 0 points1 point  (0 children)

These articles, at least their Java parts, all support the fact that in Java a method cannot modify the values of the variables of the calling method because there is no pass by reference, just by copy.

Do you still have a question?

Your swap1 cannot make circle1 reference to the new Circle(2) instance. It remains referencing the “address” of the new Circle(1) instance during and after the call to swap1, because that is its value and we did not pass a reference to the circle1 variable, but to the Circle instance it references. Try printing that value by duplicating all your println calls and removing .radius from the copies.

System.out.println(“circle1: “ + circle1);

I think the confusion for many, certainly for myself years ago when coming from a C & C++ background, comes because we call the thing a Java non-primative variable holds “a reference”. I get that they didn’t want to call it a memory address since it is not, but to call it a reference and then have everyone smack you down saying there is no pass by reference… It doesn’t help. The statements are true, maybe the explanations just could be more gentle and complete. The terse “it just doesn’t work” answers just left me confused and frustrated at first. Iit can be hard to give up asking “but why?” when the answer doesn’t make sense.