• 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

Difference between static nested class and regular class.

 
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this is a bit of a duplicate question but I want to ask it in a very specific way in order to clarify a very important point. The primary question being: Is there any difference at all between otherwise identical classes when one is a static nested class and the other is a regular, top-level, class other than access to private static fields in a containing class?



In other words: Is the only, ONLY difference, between what ContainedStaticClass can access or do and what OutsideClass can access or do, the fact that OutsideClass cannot access ContainingClass.privateStaticField directly? Or are there other, subtle differences that aren't commonly discussed or ran into?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static nested class is intended to be closely associated with its enclosing type; you should regard it as a part of the enclosing type. That is why it can access private members of its enclosing type. I presume you have already been through the Java® Tutorials about nested classes.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Grant Robertson wrote:
In other words: Is the only, ONLY difference, between what ContainedStaticClass can access or do and what OutsideClass can access or do, the fact that OutsideClass cannot access ContainingClass.privateStaticField directly? Or are there other, subtle differences that aren't commonly discussed or ran into?



You can also limit the nested classes' scope by declaring it as either private or protected.

Henry
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I generally don't access private members of enclosing or nested classes. For me the most important reasons to use static nested classes is namespacing. An example:

If I'm writing a chess game and I have chess pieces, which have a color, I could write an enum

1) Color, which I don't like because there already exists a Color class,
2) ChessPieceColor, which I don't like because of silly personal preferences, or
3) ChessPiece.Color, which clearly shows I have ChessPieces in my program, and Color is the color of a chess piece, and nothing else.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Another aspect of the access modifiers -- private means private to everything in the enclosing top-level class. This means that a nested class can access private variables of the enclosing class, of its own nested classes, and even with other nested classes that are not even in the same nesting "hierarchy".

You can argue, using C++ terminology, that all nested classes in the same top-level class are "friend" classes to each other.

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

Henry Wong wrote:
Another aspect of the access modifiers -- private means private to everything in the enclosing top-level class. This means that a nested class can access private variables of the enclosing class, of its own nested classes, and even with other nested classes that are not even in the same nesting "hierarchy".

You can argue, using C++ terminology, that all nested classes in the same top-level class are "friend" classes to each other.



This is amazing! This is one of the most helpful tidbits of information I have seen in quite some time. This could come in really handy some day.

Thanks
reply
    Bookmark Topic Watch Topic
  • New Topic