aspose file tools*
The moose likes Beginning Java and the fly likes Inner classes and static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner classes and static variables" Watch "Inner classes and static variables" New topic
Author

Inner classes and static variables

Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
I was under the impression that you can't have static variables in an inner class unless the inner class is static. Is that true or not?
Ivor Horton
Author
Ranch Hand

Joined: Mar 22, 2002
Posts: 67
That is true. Only a static inner class may have static members.


Ivor Horton<br />Author of the Beginning Java Series including the new <a href="http://www.amazon.com/exec/obidos/ASIN/1861005695/ref=ase_electricporkchop" target="_blank" rel="nofollow">Beginning Java 2 SDK 1.4 Edition</a>
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
I thought that a non-static inneer class could have static members provided that those members were compile time constants?
[ March 26, 2002: Message edited by: Dave Winn ]

Fun programming etcetera!
Ivor Horton
Author
Ranch Hand

Joined: Mar 22, 2002
Posts: 67
Dave, you are quite correct. The exception for a non-static inner class is a static field that is declared as final and initialized with a constant or a constant expression. No other kind of static members are permitted.
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
I think it's easiest to think of it as inner classes can't have static members (unless of course they are compile time constants -final members). Not to bring up a maybe more advanced topic in a beginner forum but as far as I can recall a static class inside of another class really isn't an "inner" class per se since it's not tied to an instance of that outer class. It's just a top level class that happens to reside within another class. I'm probably just making the topic more confusing so I should shut up but it might help to think of static classes all by themselves - whether they reside inside of another class or not - and then think of other non-static classes inside of another class as true inner class.
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
Thanks you guys. I wasn't aware of that exception until I got a wrong answer on a Jqplus exam. I see they were right.
Ivor Horton
Author
Ranch Hand

Joined: Mar 22, 2002
Posts: 67
No definitely should not shut up Rick - you are quite right.
The precise terminology used by Gosling is that a static class defined inside another class is called a static nested class. A static nested class is indeed just like a top-level class. Nesting inside another class only serves to qualify the class name by the outer class name.
A non-static nested class is called an inner class. This is quite a different concept since objects of the inner class type are always associated with instances of the outer class type.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner classes and static variables