Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Nested class example

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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: 10407
223
  • Mark post as helpful
  • send pies
  • 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: 65034
247
  • Mark post as helpful
  • send pies
  • 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: 65034
247
  • Mark post as helpful
  • send pies
  • 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: 10407
223
  • Mark post as helpful
  • send pies
  • 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: 65034
247
  • Mark post as helpful
  • send pies
  • 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: 10407
223
  • Mark post as helpful
  • send pies
  • 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: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry; I wasn't clear that I was agreeing with you.
 
Stephan van Hulst
Saloon Keeper
Posts: 10407
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Double subversion!
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that appears incorrect.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!