• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Inner class - question in K&B

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This doesn't even compile.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Marc and all for the help.
     
    Ranch Hand
    Posts: 637
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 7729
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And if it was posted in a block would not be indented anyway.
     
    Looky! I'm being abducted by space aliens! Me and this tiny ad!
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic