• 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

Are static nested classes related to outer?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic