• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are static nested classes related to outer?

 
Louie van Bommel
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While studying, I've referred to static nested classes as "Top level nested classes", and I haven't questioned Kathy's and Bert's passage:

and thus the static nested class does not share a special relationship with any instance of the enclosing class.


I read some of the jls, and it seems to state that there isn't any way in the world that a nested class can be referred to as a top-level class. And I played a little with a static nested class and (I think I) found out it has access to static variables in the outer class. (I hope I remembered this right

1. What does the exam/ranch call these static nested classes since jls doesn't like calling them anything related to "top-level"?
2. Do these static nested classes have any relationship with their outer class?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Java Tutorial, "a nested class is a class that is a member of another class... A static nested class is called just that: a static nested class. A nonstatic nested class is called an inner class."

Ref: http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

A static nested class is tied to the outer class rather than an instance of that class. Consequently, the static nested class does not have access to outer class instance variables, because non-static variables cannot be referenced from a static context. However, a static nested class does have access to static members of the enclosing class.

Note that a static nested class can be instantiated without an instance of the enclosing class.
[ September 16, 2004: Message edited by: marc weber ]
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy -- the phrase "top level nested class" or even worse, "top level inner class" is an old term that some at Sun used to say, but it is no longer used!

But you'll still hear it around and in books, and we've been known to say it once or twice when we aren't paying attention... just out of old habit.

The phrase "top-level" was a way of distinguishing static nested classes from non-static nested classes, to give you the impression that static nested classes behaved more like plain old non-inner/nested classes.

But there was never any other special type of nested class. A "top-level nested class" or "top-level inner class" was always just another way of saying "static nested class". And sometimes we even used the (much worse) phrase, "static inner class". It was never really appropriate to call static nested classes "inner" classes, but it happened...

So, it was just bad/inconsistent naming, but some of us working at Sun got into the bad habit of using it and haven't always been good about breaking the bad habits

Cheers,
Kathy
 
Louie van Bommel
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
[QB]static nested class is tied to the outer class rather than an instance of that class... [and] does have access to static members of the enclosing class.QB]


Thanks Marc for clearing that up. I always get a good explanation from your posts

Thanks Kathy for commenting on why I continue to see that (now evil ) term referring to static nested classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic