jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class - question in K&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class - question in K&B " Watch "Inner class - question in K&B " New topic
Author

Inner class - question in K&B

jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
chapter 8: Inner classes

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?
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
This doesn't even compile.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


  • "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    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
    Thanks Marc and all for the help.
    Stuart Ash
    Ranch Hand

    Joined: Oct 07, 2005
    Posts: 637
    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
    And if it was posted in a block would not be indented anyway.


    Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
    Getting someone to think and try something out is much more useful than just telling them the answer.
     
    Don't get me started about those stupid light bulbs.
     
    subject: Inner class - question in K&B
     
    Similar Threads
    Inner Classes
    Why anonumous inner class is not take the ";"
    InnerClass question from Ch8 SierraBates
    Inner Classes Mock question doubt 3
    Inner Class k&B page 483