File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class - question in K&B

 
jibs parap
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't even compile.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  •  
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 134
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Marc and all for the help.
     
    Stuart Ash
    Ranch Hand
    Posts: 637
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Barry Gaunt
    Ranch Hand
    Posts: 7729
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And if it was posted in a block would not be indented anyway.
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic