Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

shadowing object reference variables

 
krithika naya
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Bar {
int barNum = 28;
}

class Foo {
Bar myBar = new Bar();
void changeIt(Bar myBar) {
myBar.barNum = 99;
System.out.println("myBar.barNum in changeIt is " + myBar.barNum);
myBar = new Bar();
myBar.barNum = 420;
System.out.println("myBar.barNum in changeIt is now " + myBar.barNum);
}
public static void main (String [] args) {
Foo f = new Foo();
System.out.println("f.myBar.barNum is " + f.myBar.barNum);
f.changeIt(f.myBar);
System.out.println("f.myBar.barNum after changeIt is "
+ f.myBar.barNum);
}
}
The preceding code prints out this:
f.myBar.barNum is 28
myBar.barNum in changeIt is 99
myBar.barNum in changeIt is now 420
f.myBar.barNum after changeIt is 99


how it is possible to get 99 atlast?
please explain this.
 
Jari Timonen
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you what you pass to a method is COPY of reference variable!

If you make a new object to COPY of reference variable, you're assigning new object to the COPY, not to original.

Also..
Primitive variables(int,long etc.) are just reference variables. They don't have object in heap. They only lie in stack. Try passing primary variable to method, change the value and print the original. Original DOES NOT change. The same is happening with objects when you try to change the reference. You try to change the reference of the reference variable to a new object, but it's only a COPY, so the original object won't be affected.

If you use the original reference variable to change the value of attribute (myBar.barNum = 99 , it's changed since your using the original object.

Was that confusing enough?
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jari, you may want to be careful with your usage of the term "reference variable". The sentence "primitive variables(int,long etc.) are just reference variables" isn't correct in the typical sense in which Java programmers usually use the word "reference". You probably mean to talk about pass-by-value vs. pass-by-reference function call semantics.


Anyway, to the original poster, the key issue is that Line A (as referenced above) does not change the f.myBar field in main(). Instead, it only changes the local variable (or formal parameter) named "myBar" in the changeIt() method. It's probably easier to see the problem if you realize that you can actually rename changeIt's "myBar" parameter to any other identifier. In other words, the following code fragment is completely equivalent to the one above:


[ October 26, 2007: Message edited by: Kelvin Lim ]
 
Jari Timonen
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing(?) things.

Please read: http://www.javadude.com/articles/passbyvalue.htm
[ October 26, 2007: Message edited by: Jari Timonen ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic