aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes shadowing object reference variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "shadowing object reference variables" Watch "shadowing object reference variables" New topic

shadowing object reference variables

krithika naya

Joined: Oct 25, 2007
Posts: 10
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);
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

Joined: Jan 16, 2004
Posts: 232
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.

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?

SCJP 5.0, SCJD URLyBird 1.3.3, OCMJEA 5 (SCEA) Factory Homes
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
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 ]

SCJP 5.0
Jari Timonen
Ranch Hand

Joined: Jan 16, 2004
Posts: 232
Thanks for clearing(?) things.

Please read:
[ October 26, 2007: Message edited by: Jari Timonen ]
Don't get me started about those stupid light bulbs.
subject: shadowing object reference variables