• 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

Nested class example

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, i was reading about nested classes and I came accross this example that i found interesting. How valid/accurate/good practice is it? :

"Another possible use of a nested class is when it does not make sense for the class to be a seperate entity, since it serves as a helper to another class. Let's imagine a simplified example of an "Application" class. In order to correctly configure the settings of an application (eg. it's the Dimesions), we may need some information from the user that doesn't have a direct relation with our "Application" class (for example,we may need to know the dimensions of the user's device in order to set up the dimensions for our application). For this reason we use the nested class "ApplicationSettings" that is used as another abstraction layer that provides useful information for our "Application" class :"
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

It's fine, but it's a pity they called the nested class ApplicationSettings, because the scope that the enclosing Application class provides makes this redundant. What's easier to read: Application.ApplicationSettings, or Application.Settings?

Other examples that I like are when you have an enum that is strongly related to some specific class, then you can nest the enum. For instance a ChessPiece.Color, or a Card.Rank and a Card.Suit. A nice example from the standard API is Map.Entry.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You often see exercises to write a linked list from scratch, or a binary search tree. You would want a Node class, which you would only use internally, so it would make sense to make that a private inner class.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I often see exercises to write a stack, a linked list, or a binary search tree from scratch. Since a node is part of such a data structure, and not used outside, it would make sense to have a private inner Node class. Of course LinkedList.Node, Stack,Node, and BnaryTree.Node would differ from one another.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:it would make sense to make that a private inner class.


It would make more sense to make it a private static nested class.

"Inner class" refers to a non-static nested class, and the node of a linked list or tree doesn't need a reference to the list or tree it's a part of.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it doesn't. The tree needs a reference to the nodes, but not vice versa.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is exactly what I said, and also the reason why the node class can be made static, meaning it's not an inner class.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry; I wasn't clear that I was agreeing with you.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double subversion!
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Sorry; I wasn't clear that I was agreeing with you.



It seems like you couldn't fail to avoid disagreeing with him less.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that appears incorrect.
 
Would you like to try a free sample? Today we are featuring tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic