wood burning stoves
The moose likes Java in General and the fly likes Static memeber inside Inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static memeber inside Inner classes" Watch "Static memeber inside Inner classes" New topic

Static memeber inside Inner classes

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 235

Hi all,
I have a little doubt regarding the use of the static member in the inner class.
I have read that the it is not allowed because inner class can not be accessed without creating object of outer class.Fine.But what does this have to do with inner class's static member.I mean, that is associated with the inner class and not the outer class.So it should be inner class's instance creation we should be thinking of .In other way, why are we concluding the non-allowance of use of static member inside inner class based on the outer class's instance creation?

Please clear my doubt.


Steve Luke

Joined: Jan 28, 2003
Posts: 4181

It is all about the static context. A static context exists only as long as there is no enclosing instance.

For every inner class there is an enclosing instance of the outer class, so the 'static context' is broken - it can't exist. If you think about what the static context for an inner class would look like, you start to understand. Because each inner class is in the context of an enclosing instance of the outer class the inner class' static context would also be enclosed by that outer class instance. Then you would have a different static context for each outer class instance that is created, and you would lose the whole 'static' behavior of the static context relative to instances of the inner class created in different instances of the outer class (that is, a static context for the inner class would be static for all instances of the inner class created from the same outer class instance, but would be different from the static context of inner class instances created from a different outer class instance).

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
An inner class does not necessarily have an instance of an enclosing class. And no, I'm not talking about an "inner class" that is really a static nested class. I'm talking about actual inner classes, as defined in the Java Language Specification, that are declared ina static context, as that term is defined in the Java Language Specification. An inner class defined in a static context is still considered an inner class, even though there is no enclosing instance.

Among other things, this means that an inner class in a static context cannot declare a static member - even if there seems no good reason not to allow it. It certainly would have been possible to allow static members. And in fact, it's possible to inherit static members from non-inner classes that you extend. But they don't want you declaring new ones, I think they did this for simplicity. If an inner class does have an enclosing instance (as it usually does), would a static member of that inner class have a one t- one association with the instance of the outer class, or with the outer class itself? That's a mildly confusing issue which they might need to explain to people with a special rule. Instead they chose to simply ban static members of inner classes. And there's really no need for a static member in an inner class. Any data you want in a static field of an inner class, you can just put it in a static field of the enclosing class.
I agree. Here's the link: http://aspose.com/file-tools
subject: Static memeber inside Inner classes
It's not a secret anymore!