• 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

Why Is There This Xtra Code In K&B Book Sample?

 
hangman
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much for any hints on what I am missing here my friends, but check out the small code sample on p. 463 of Chapter 8 on "Inner Classes." I can't for the life of me figure out the purpose of the makeInner() method starting on line 3. It does absolutely nothing, but throw me off from trying to understand the real point of what K&B are trying to convey.

I tried taking the method out and the code still compiles and runs with the exact same results as printed in the book.

Nor is there any text in there that says "be careful of questions that try to throw you off with do-nothing code.." or anything like that.

Thanks so much to anyone who can explain this.
--BobN

class MyOuter {
private int x = 7;
public void makeInner() {
MyInner in = new MyInner();
in.seeOuter();
}
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Inner class ref is " + this);
System.out.println("Outer class ref is " + MyOuter.this);
}
}
public static void main (String[] args) {
MyOuter.MyInner inner = new MyOuter().new MyInner();
inner.seeOuter();
}
}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Certainly it is not used. But it does not do nothing: You can use it thus:


Probably it was left in from the time they were learning the stuff themselves
 
reply
    Bookmark Topic Watch Topic
  • New Topic