• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Anonymous and Static classes  RSS feed

 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

Can you please explain to me, what is the common uses of Anonymous classes and Static classes?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Comparator is commonly implemented as an anonymous class, because it doesn't really carry any logic and is mostly done in 2 lines of code. Also, check here http://www.gawer.org/books/OReilly_Reference_Library/javaenterprise/jnut/ch03_12.htm

as for static inner classes, you use them when there is no need to have a reference of the enclosing class if you want to use the inner class.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any class instance can be anonymous; an object reference without a name. This is
useful in GUI code, for example, where many listener objects are often needed.
Below, "new JButtonListener()" creates an anonymous class that is passed directly
to the button. (It gets a name in the JButton constructor.) I/O operations use a lot
of anonymous classes too.

Sebastian explained static nested classes (the non-static kind are called inner classes).
But if you are asking more generally about static class members (attributes, methods,
static nested classes), they provide singleton behavior for your application.

Jim... ...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!