This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

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

 
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 ]
 
Sheriff
Posts: 21866
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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!
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!