This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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?
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."
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 ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
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
Louie van Bommel
Joined: Aug 17, 2004
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.
subject: Are static nested classes related to outer?