• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static memeber inside Inner classes

 
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic