• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

"Shadowing" Question

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is coming from the 1.4 Bates book (pg. 187-188)...Can you explain where the reference is being changed? My guess is where the * are..help please!

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 " + barNum);
* myBar = new Bar();
myBar.barNum = 420;
System.out.println("myBar.barNum in changeIt is now " +barNum);
}
public static void main(String args[])
{ Foo f = new Foo();
System.out.println("f.myBar.barNum is " + f.myBar.barNum);
* changeIt(f.myBar);
System.out.println("myBar.barNum after changeIt is " + f.myBar.barNum);
}

Code prints:

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
 
Cheryl Gray
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to my question(sorry, missed this), I would like to know where the f.myBar.barNum was changed to formerly produce the output of 28 and now to 99. I'm thinking it's occurring where the * are. Am I correct?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume that your code should look like this.



I think most of the print statements are straightforward.

In regards to where f.myBar.barNum is changed, when the method changeIt begins executing, the formal parameter myBar is a reference to the object referred to by f.myBar, so the first line in the method will change the value of barNum to 99.

After you point the formal parameter to a new object inside the method, it no longer refers to the original object so the line



will have no effect on the object referred to by f.myBar.
 
Cheryl Gray
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your input!
 
Cheryl Gray
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your input! I now understand the code...
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic