Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > inner classes and static members

Reply
Thread Tools

inner classes and static members

 
 
maxw_cc
Guest
Posts: n/a
 
      12-03-2003
Can somebody please tell me a practical reason why you couldn't have
static members (not constants) within an inner class? I know that this
is forbidden, but I haven't found a practical, concrete reason for
this... I was wondering if this was just a whimsical decision from the
designers of the language.

Thanks a lot in advance...
 
Reply With Quote
 
 
 
 
Andy Fish
Guest
Posts: n/a
 
      12-03-2003
IIRC you can have a static member within a _static_ inner class.

If the inner class is not static, I'm not sure it makes sense to have a
static member. This would rightly be a member of the owning class.

"maxw_cc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Can somebody please tell me a practical reason why you couldn't have
> static members (not constants) within an inner class? I know that this
> is forbidden, but I haven't found a practical, concrete reason for
> this... I was wondering if this was just a whimsical decision from the
> designers of the language.
>
> Thanks a lot in advance...



 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      12-03-2003
maxw_cc wrote:

> Can somebody please tell me a practical reason why you couldn't have
> static members (not constants) within an inner class? I know that this
> is forbidden, but I haven't found a practical, concrete reason for
> this... I was wondering if this was just a whimsical decision from the
> designers of the language.


Put it down to whimsy.

I think there was some feeling that it couldn't be needed, and that forbidding
static members made the construct somehow "cleaner" (which is laughable).
There is no *technical* reason for it in the sense that the current
implementation would work just as well if static members were allowed.

-- chris


 
Reply With Quote
 
xarax
Guest
Posts: n/a
 
      12-03-2003
"maxw_cc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Can somebody please tell me a practical reason why you couldn't have
> static members (not constants) within an inner class? I know that this
> is forbidden, but I haven't found a practical, concrete reason for
> this... I was wondering if this was just a whimsical decision from the
> designers of the language.
>
> Thanks a lot in advance...


The practical answer is that Sun couldn't decide on the best
time to initialize the static fields of the inner class. There
are complicated issues when initializing static fields. It's
not simply a matter of "initialize all static fields of all
inner classes when the enclosing class is initialized", because
it gets very complex (you can have any number of enclosing
classes). They just threw up their hands and said "don't allow
it for now". A static nested class can be initialized on its
first reference, just like any other "normal" class, but an
inner class presents some complex issues regarding timing.

2 cents worth. Your mileage may vary.


 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      12-03-2003
Chris Uppal wrote:

> maxw_cc wrote:
>
>
>>Can somebody please tell me a practical reason why you couldn't have
>>static members (not constants) within an inner class? I know that this
>>is forbidden, but I haven't found a practical, concrete reason for
>>this... I was wondering if this was just a whimsical decision from the
>>designers of the language.

>
>
> Put it down to whimsy.
>
> I think there was some feeling that it couldn't be needed, and that forbidding
> static members made the construct somehow "cleaner" (which is laughable).


I think the prohibition protects novice and journeyman programmers who
might be inclined to assume that inner classes' static members (if they
could have any) were scoped to a particular enclosing class instance.
The inner class itself is not static after all. (Weak, I know.)

Certainly it is the case that since all inner classes must have a static
or top-level containing instance, the inner class' static data can be
assigned to the class of that containing instance.

> There is no *technical* reason for it in the sense that the current
> implementation would work just as well if static members were allowed.


Yes, that's absolutely true. I do wonder whether the designers at the
time were also trying to hedge against a future implementation where the
difference might matter, however.


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      12-03-2003
John C. Bollinger wrote:

> > There is no *technical* reason for it in the sense that the current
> > implementation would work just as well if static members were allowed.

>
> Yes, that's absolutely true. I do wonder whether the designers at the
> time were also trying to hedge against a future implementation where the
> difference might matter, however.


That's a good point.

Personally, I think the implementation is pretty much set in stone, but the
fact that *I* think that *now* doesn't mean that *they* thought that *then*

-- chris



 
Reply With Quote
 
James Brady
Guest
Posts: n/a
 
      12-04-2003
On Tue, 02 Dec 2003 22:27:18 -0800, maxw_cc wrote:

> Can somebody please tell me a practical reason why you couldn't have
> static members (not constants) within an inner class? I know that this
> is forbidden, but I haven't found a practical, concrete reason for
> this... I was wondering if this was just a whimsical decision from the
> designers of the language.
>
> Thanks a lot in advance...


An inner class can access the non-static fields/methods of the containing
object. This makes the inner class relevant only to the containing class.
Therefore, to have a static inner field would mean one single field being
updated according to the state of many different, diverse, objects.

This is not only a horrible programming gotcha to spot/debug, it is (I
would suggest) an abuse of the data encapsulation Java is built on and
it is certainly useless. If you want a static field, just use one in the
containing class!!

HTH
Jim
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why can nsmc, local classes or anonymous classes have static members? Rit Java 23 01-03-2010 05:54 PM
Why inner classes can not have static members? bearice@gmail.com Java 8 02-09-2008 03:02 AM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments