This week's giveaways are in the MongoDB and Jobs Discussion forums. We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line! See this thread and this one for details.
Question 8: public class Foo{ Foo(){Sytstem.out.print("foo");} class Bar{ Bar(){Sytstem.out.print("hi");} }
public static void main(String[] args){ Foo f = new Foo(); f.makeBar();
void makeBar(){ (new Bar()){}).go(); } } } -----------------
Just trying to understand the line: (new Bar()){}).go(); This is considered to be an anonymous class but at the same time is this is a mathod-local inner class as well? [couldn't find any such examples in the book either under anonymous class or method-local class] Can anyone give a better explanation?
Originally posted by jibs parap: ... This is considered to be an anonymous class but at the same time is this is a mathod-local inner class as well?
This anonymous class definition happens to be within a method body (once you've corrected the details), but it's not technically a "local class." According to JLS - 14.3 Local Class Declarations, "A local class is a nested class (�8) that is not a member of any class and that has a name." [ April 14, 2007: Message edited by: marc weber ]
jibs parap
Ranch Hand
Joined: Dec 07, 2004
Posts: 134
posted
0
Thanks Marc and all for the help.
Stuart Ash
Ranch Hand
Joined: Oct 07, 2005
Posts: 637
posted
0
Originally posted by marc weber: The code you've posted has:
One misplaced brace.
One missing brace.
One extra parenthesis.
A misspelled class name (twice).
A call to a method that doesn't exist.
And NOT posted in the code block.
ASCII silly question, Get a silly ANSI.
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
And if it was posted in a block would not be indented anyway.