Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Class - K&B - Page 483

 
suriya indu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the Code from K&B's inner class section (Page 483)

I am not able to under stand the syntax (new Bar(){}).go(); used to invoke the inner class method. My understanding is either (new Bar()).go();
or 1. Bar in = new Bar(); 2. in.go() is the right way to invoke the method go() of the inner class. Confusion is the {} braces after the new Bar(){}.
Can any one explain me this please. My Head is spinning with inner class concept
[ March 06, 2004: Message edited by: suriya indu ]
[ March 06, 2004: Message edited by: suriya indu ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The line you mention: (new Bar(){}).go(); creates an anonymous inner class that extends the inner class Bar. It is no different than just writing (new Bar()).go(); since the go method is not overriden. This may make more sense to you:

[ March 06, 2004: Message edited by: Michael Morris ]
 
Suresh Thota
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suriya,
Please check this.
http://www.coderanch.com/t/241051/java-programmer-SCJP/certification/InnerClass-Ch-SierraBates
There is a previous discussion about the above question.
Cheers
-Suresh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic