• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

shadowing object reference variables

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic