• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner classes  RSS feed

 
Manick Batcha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what situation is an anonymous inner class preferred over an ordinary inner class and vice versa?

Why can't a inner class have static data members??

Ranchers can you please clarify my queries??
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

use an anonymous inner class iff you only have to create instances in one point and there is an appropriate type.

Conan
 
Erik Larson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran into the problem with static data in an inner class recently.

This site

Nested Classes

gives a good short description of inner classes. A non-static inner class is associated with an instance of a class, not the class itself. That's why the inner class can access "this" for the enclosing class. So declaring something static in the inner class would only be accessable to that instance, which defeats the purpose of it being static. If you don't need direct access to the enclosing class instance (this, or its members directly), try declaring the inner class static.
[ May 17, 2005: Message edited by: Erik Larson ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!