File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How do I Read this? 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 "How do I Read this?" Watch "How do I Read this?" New topic
Author

How do I Read this?

Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
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?
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
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.
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
The anonymous class is a subclass of the class Bar, not the class Sample.


SCJP 1.4 - 95%
Ramamoorthy Periasamy
Ranch Hand

Joined: Feb 06, 2006
Posts: 30


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

Joined: Jul 20, 2006
Posts: 107
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I Read this?
 
Similar Threads
Accessing the new method in the anonymous inner class
Anonymous inner class ....
Anonymous Inner Class
An anonymous class can be static?????
Question on Anonymous Inner class