• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner classes; when should they be static vs. instance?  RSS feed

 
dave taubler
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a question I've wondered for some time, so I thought I would just ask to see if anyone had a good answer.

When you create an inner class, you can make it either static or instance-specific. e.g.


which gets created via new MyOuterClass.MyIterator()
versus

which gets created via new MyOuterClass().MyIterator()

Simply asked, are there situations where it's better to use a static inner class, and situations where it's better to use the instance-based approach? I find myself always using static inner classes, since it's easier to invoke, but I wonder if I'm missing some drawbacks I might be incurring.
[ November 10, 2005: Message edited by: dave taubler ]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Non static) Inner classes have full access to any (even private) members of its enclosing class instance. Static inner classes (actually Nested classes) can only access static members of its enclosing class.
 
dave taubler
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!