Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Inner Class

 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, it depends by what you mean by "local". That statement doesn't contain the word "method".
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic