This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 74371
334
  • 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!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic