• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why can't we define static members is Inner class..?  RSS feed

 
Naveen Raja
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I did not understand why static members(var or methods) can not be defined in Inner class,where as static final members can be?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

I don't think there's a compelling reason, other than "that's just the rule." As inner classes have full access to data in their outer class, you can just put the statics there, of course.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's because the semantics wouldn't be fully clear. Would a static variable for an inner class exist only once per VM (that is, classloader), or once per outer instance? Technically, it would be the former, but it could be argued that logically it *should* be the latter. Sun just decided to circumvent the problem by not allowing it at all.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have an static member you do not need to instantiate the class to use it. Now, the only way to use an nested inner class is by means of instantiation. Therefore, what would be the point of static members in shuch kind of classes when the only way to use it is by means of instantiating it?

However, inside the inner class you can use static members declared in the outer class.

Members declared as static final are allowed inside inner class as long as they evaluate to a constant expression (JLS 15.28). Why is that, I have no idea. Maybe to simplify the use of constants within the inner class.
[ July 28, 2006: Message edited by: Edwin Dalorzo ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edwin Dalorzo:
When you have an static member you do not need to instantiate the class to use it. Now, the only way to use an nested inner class is by means of instantiation. Therefore, what would be the point of static members in shuch kind of classes when the only way to use it is by means of instantiating it?

However, inside the inner class you can use static members declared in the outer class.

Members declared as static final are allowed inside inner class as long as they evaluate to a constant expression (JLS 15.28). Why is that, I have no idea. Maybe to simplify the use of constants within the inner class.

[ July 28, 2006: Message edited by: Edwin Dalorzo ]


I doubt that's the reason. If they allowed it the access would merely have to be qualified, e.g. Outer.Inner.STATIC_VAR. While I've never seen a reason given by any authoritative source I think Ilja's probably closest to the truth. Furthermore, allowing compile-time constants would seem to support that idea because that's the one case where the semantics wouldn't matter, it's always the same value regardless of how the semantics are interpreted.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!