aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why not static members in inner classes? 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 "why not static members in inner classes?" Watch "why not static members in inner classes?" New topic
Author

why not static members in inner classes?

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Q1: Why are static members not allowed to be declared in inner classes?
Q2: Why are static members allowed to be inherited by inner classes if they are not allowed to be declared in inner classes?
[ April 25, 2003: Message edited by: Marlene Miller ]
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy
A static member has to be accessible *without* necessarily having an instance of the class with the static member. But... an inner class (non-static nested class) has no meaning in life until it's instantiated.
But the inner class can *inherit* them, because once the inner class is instantiated, it has access to everything in the enclosing class instance to which the inner class instance is tied. So to be a truly good 'helper' to the enclosing class *instance* (which is the way I think about inner class objects-- as helpers to the outer object), it would need access to everything, statics included. But again, since an inner class has no meaning until its instantiated, there'd be no point in putting statics in it.
Does that make any sense? I've never actually thought about this, although it seems like a pretty significant point!
cheers,
Kathy
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Don't you like Kathy's one?


SCJP2. Please Indent your code using UBB Code
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Jose Botella:
Don't you like Kathy's one?

Yes, I do like Kathy's post but I started to write it before Kathy posted her's. Then I deleted mine before you posted yours.
I actually started to write an explanation similar to Kathy's but then I backed away from it because I thought that I might be starting a long debate on the design choices for Java.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
It happens to me all the time.
[ April 25, 2003: Message edited by: Jose Botella ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Kathy. I understand what you mean by a helper object. That makes sense.
Suppose several inner class objects wanted to share some class data, such as an identifier or an ArrayList.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why not static members in inner classes?