• Post Reply Bookmark Topic Watch Topic
  • New Topic

non-static inner classes  RSS feed

 
Lalit Kapoor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Non-static inner classes cannot have static variables. Why?
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may be able to answer this question yourself: What would such a static member variable do, that couldn't be done with a static member of the outer class?

M
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some additional food for thought... and what can a static member variable of a static nested class do, that couldn't be done with a static member of the outer class?

- Peter
[ June 07, 2004: Message edited by: Peter den Haan ]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent question. I couldn't think of any particularly strong reasons. The problem with guessing what the language designers intended is that only the language designers actually know. So...I sent an email to James Gosling. His response:


This was one of those late-in-the-game decisions that created lots of
heated debate. (early decisions were easier because there was just me, so the
only one I had to argue with was myself) It was also long enough ago that my
memory is pretty hazy. Josh, Gilad & Guy: if I get this wrong, please chime
in!

As I remember it, the decision revolved around the question "what would static mean in such a class". One answer could be that it's just what it means in a top-level class, and in retrospect I think that's the interpretation we should probably have gone with. Another answer, that is probably more "right" in an intellectual sense, is that the containing class is roughly the "static context" of the inner class, so the static field should really be an instance variable in the containing class. This had enough issues that we decided that the clearest and simplest thing to do was to just avoid the issue altogether and disallow them.


It's also possible that a subtle implementation/specification issue influenced the decision. If that's the case, I may hear from someone else soon with details.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I thought about the matter, I couldn't come up with an implementation reason why an inner class couldn't have static members; after all, it is implemented as a static nested class that gets an implicit reference to the enclosing instance. You could implement this explicitly and create an "inner class" with static members. Which leaves the difference between the way static and non-static inner classes (as a class) relate to instances of the enclosing class on the language level.

- Peter
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!