• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How do I Read this?

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from the k&b
void makeBar() {
(new Bar(){}).go();
}
Is this is method local inner class or an anonymous class? If its anonymous how is it being declared inside a method? what exactly is going on here?
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding some code around the fragment that you have given, to give it some contextual meaning



Observe that inside the makebar() method, we are creating an anonymous inner class, which is the subclass of class Sample. This inner class doesnot override any methods from the superclass. And then we just call the go() method on the newly created instance.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The anonymous class is a subclass of the class Bar, not the class Sample.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


class Bar might have declared as abstract to be more appropriate. Then only its a worth creating a anonymous class (which is a subclass of Bar).
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Douglas Chorpita:
The anonymous class is a subclass of the class Bar, not the class Sample.


Yes, True, Typo on my part. Thanks for pointing out.
 
reply
    Bookmark Topic Watch Topic
  • New Topic