• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner Classes vs Static Nested Classes

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




From here I got a nitpick suggesting that I make these inner classes (as opposed to static nested classes). I had made them static because they don't need a reference to the surrounding object. What is the advantage here of using inner classes?
 
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static nested classes cannot access non static members of the outer class. Will this be a disadvantage in your design?
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shuba gopal wrote:Static nested classes cannot access non static members of the outer class. Will this be a disadvantage in your design?

No, there is no need to ever create an instance of the outer class.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a class(say A) is useful only to other class(say B), then better make A as an inner class(non-static) to B. since you are not needed the inner class object outside of the A. also you can access the outer class instance variable in A.
otherwise consider to make the inner class as static . so that you can avoid the outer class object creation to access the nested class from another top-level class(say C).
 
shuba gopal
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Seetharaman says, if you will not have any nested classes that refer to Lookup6 instance variables or instance methods then you can use static nested classes.

But having everything as static, you will not be able to take advantage of many aspects of OOP dont you think? In terms of design(imho) you should account for possible future improvements to the program. If everything is static it is not very different from programs in languages that do not use OOP.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult a question for "beginning". Moving discussion.
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shuba gopal wrote:But having everything as static, you will not be able to take advantage of many aspects of OOP dont you think? In terms of design(imho) you should account for possible future improvements to the program. If everything is static it is not very different from programs in languages that do not use OOP.

Very true. If I was allowing for improvements I would start by moving Thing and Video (etc) outside the Lookup program for use by other portions of a system, and I wouldn't be storing my inventory in a static HashMap. Thanks to all for the advice!
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic