aspose file tools*
The moose likes Java in General and the fly likes Anonymous Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Anonymous Inner Class" Watch "Anonymous Inner Class" New topic
Author

Anonymous Inner Class

Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Please help me understand inner classes better. Java in a nutshell says that an anonymous inner class is a local inner class without a name. How can it be local when it can be defined outside a method? For example,

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

ogeh ikem wrote:an anonymous inner class is a local inner class without a name.

I never heard/read . however in java, technically every class has a name. just compile the class and see the syntactic name for the anonymous class
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

They're most commonly used within methods. But they don't have to be, and you've shown an example where they aren't. An anonymous inner class can be created anywhere where you can have an executable statement. In your case, you could still consider it local to that particular initialization line.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
So the statement made by Java in a Nutshell
an anonymous inner class is a local inner class
is not an accurate statement.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

As I said, it depends by what you mean by "local". That statement doesn't contain the word "method".
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
A local variable implies that it is declared inside a method. The same goes for a local inner class. Is there a different meaning of the term local?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

ogeh ikem wrote:A local variable implies that it is declared inside a method. The same goes for a local inner class. Is there a different meaning of the term local?


Local actually means local to a block -- any block. So, you can have local classes in a method, in a constructor, or in an initializer. Anonymous inner classes behaves the same way, and hence, can also be used local to a block. The reason anonymous inner classes can also be used with instance variables (as in your example) is because local inner classes can't be declared and used in the same line of code.


Now, having said that, if you can declare a local inner classes; and then keep it in scope for an instance variable, then yes, it would be possible. However, it isn't possible for the same class. The best you can do is to keep it in scope for another inner class, like so...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

BTW, if you will allow the use of an instance initializer, then you can assign a local inner class to a instance variable -- and almost at the same time as the declaration. It just can't be done in one line.



Henry
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Your examples have demonstrated the fact that local inner classes are truly local ie they can only be declared inside a block. However, I'm simply questioning the accuracy of the statement
an anonymous inner class is a local class without a name
I don't think that it's a very accurate statement because it seems to suggest that, like local inner classes, anonymous inner classes can only be declared inside a block. This is not accurate because we know that anonymous inner classes can also be declared outside a block. Local inner classes on the other hand, cannot be declared outside a block.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Complete Java 2 Certification Study Guide, 5Ed [Sybex 2005] says
Some classes that you define inside a method do not need a name. A class defined in this way without a name is called an anonymous class. Anonymous classes can be declared to extend another class or to implement a single interface.
This doesn't sound accurate either because it suggests that anonymous classes are confined to blocks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

ogeh ikem wrote:Complete Java 2 Certification Study Guide, 5Ed [Sybex 2005] says
Some classes that you define inside a method do not need a name. A class defined in this way without a name is called an anonymous class. Anonymous classes can be declared to extend another class or to implement a single interface.
This doesn't sound accurate either because it suggests that anonymous classes are confined to blocks.


I think you might want to consider contacting the publisher, and filing an errata request. It definitely can't hurt to do so.

IMHO though, I think the point is too subtle. And I don't have a strong opinion on regards to whether the statement is accurate or not.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Inner Class