File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Are static nested classes related to outer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Are static nested classes related to outer?" Watch "Are static nested classes related to outer?" New topic
Author

Are static nested classes related to outer?

Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"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
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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

Joined: Aug 17, 2004
Posts: 76
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are static nested classes related to outer?