• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Page 188 of K&B

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the piece of code under "From the class room" there is a variable barNum (of class Bar) being accessed in the changeIt method of class Foo which doesnt extend the class Bar. There is no local variable defined that i can see.. (unless i need an extra pair of my granny's glasses) .. does this code compile at all ?
i dont think so.. how about people who churned this book? Or am i being dumb .. missing out something here..



To error is human.


So please bear with me if it were a stupid post. i'll be glad to take any kind of response. thanks
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, There is something missing in this code. I just chnaged this code without using inheritance.

class Bar {
int barNum = 28;
}

class Foo{
Bar myBar = new Bar();
static void changeIt(Bar myBar) { //Changed the method as static
myBar.barNum = 99;
System.out.println("myBar.barNum in changeIt is " + myBar.barNum); //changed barNum as myBar.barNum
myBar = new Bar();
myBar.barNum = 420;
System.out.println("myBar.barNum in changeIt is now " + myBar.barNum); //changed barNum as myBar.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);
}
}

Raghu J
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i havent tried to check if the code compiles, however I partially agree with Raghu's code modification, except for the part where he has declared the method to be static,i don't see the reason for doing so.

Can you please explain.

Thanks

Kiennjal
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


where he has declared the method to be static,i don't see the reason for doing so.



Nothing special reason. That code also run without static declaration. I agree your doubt. Here changeIt() method declared as static is not needed.



Raghu J
 
reply
    Bookmark Topic Watch Topic
  • New Topic