This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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


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.
     
    GeeCON Prague 2014
     
    subject: Inner class - question in K&B