Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner classes and static variables

 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is true. Only a static inner class may have static members.
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ivor Horton
Author
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic