• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

"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...
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic