• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How do I Read this?

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic