From: Rit on
Hi All,

I could not understand why nsmc, local classes or anonymous classes
cannot have static members ?

Can some throw light on that ?

Thanks,
Ankur
From: Rit on
On Dec 12, 1:49 pm, Arne Vajhøj <a...(a)vajhoej.dk> wrote:
> On 12-12-2009 16:27, Rit wrote:
>
> > I could not understand why nsmc, local classes or anonymous classes
> > cannot have static members  ?
>
> > Can some throw light on that ?
>
> Anonymous classes can not have static methods because those
> can not be called by anyone.
>
> I don't know what you mean by nsmc and local classes.
>
> Arne

By nsmc I mean non static member classes.

By local classes I mean classes declared in local blocks.

Rit
From: Rit on
On Dec 12, 1:52 pm, markspace <nos...(a)nowhere.com> wrote:
> Rit wrote:
> > Hi All,
>
> > I could not understand why nsmc, local classes or anonymous classes
> > cannot have static members  ?
>
> > Can some throw light on that ?
>
> Well, I don't know, but I'll hazard a guess or two:
>
> 1. Conceptually, non-static member classes, local and anonymous classes
> are all "a part of" the enclosing class.  Thus they "should" be using
> the enclosing class for any static members.
>
> 2. For future potential optimization, preventing static members might
> allow future JVMs to take some short cuts with inner class objects, and
> initialize the objects more quickly or otherwise do away with one or
> more steps involved in creating their class object.  Since static
> members weren't needed (see 1), they were proscribed.

Thanks for your response. Local classes in static context do no have
an enclosing object. What could happen if nsmc, local classes or
anonymous classes had static members. Why can't Local classes in
static context have static members at least ?

Rit
From: Arne Vajhøj on
On 12-12-2009 17:15, Rit wrote:
> On Dec 12, 1:49 pm, Arne Vajh�j<a...(a)vajhoej.dk> wrote:
>> On 12-12-2009 16:27, Rit wrote:
>>> I could not understand why nsmc, local classes or anonymous classes
>>> cannot have static members ?
>>
>>> Can some throw light on that ?
>>
>> Anonymous classes can not have static methods because those
>> can not be called by anyone.
>>
>> I don't know what you mean by nsmc and local classes.
>
> By nsmc I mean non static member classes.

Ah.

Somebody made a decision.

Given that such classes are tied to something non-static (the
instance of the surrounding class), then the use of static would
be bit fuzzy in semantics.

> By local classes I mean classes declared in local blocks.

Forgot about those bastards.

I think they are mostly a certification thingy. I don't think I
have ever seen real world code using it.

Same applies as above.

Arne

From: Lew on
Rit wrote:
> Thanks for your response. Local classes in static context do no have
> an enclosing object. What could happen if nsmc, local classes or
> anonymous classes had static members. Why can't Local classes in
> static context have static members at least ?

I believe the other respondents have already addressed this. I'll add that
since inner classes are inner classes are inner classes, it saves a lot of
complexity not to have different rules for them when they're in a static context.

Another consideration is to think through what a (non-constant) static member
would signify for an inner or local class. What does "class-wide scope" even
mean there? The scope is local to begin with, so adding class-wide scope to a
local-scoped construct is dicey at best.

If one has a need to refer to a class widely, it should be neither local nor
inner with an enclosing instance anyway, ergo the utility of a static member
is nil. If you need a static member, declare a top-level class or static
nested class to hold it. If you need it in a local/inner class, put it in the
enclosing class. Having it in the inner class adds negligible value to the
language, and would require complexity to implement. The cost-benefit
analysis doesn't justify it. We may never know what the Founding Designers
thought about the issue, but we can guess that they felt much the same.

Given that the language prohibits static (non-constant) members in an inner
class, and the workarounds are simple and straightforward, I'd rate the reason
behind the decision as not very critical. I know that I find types much
easier to reason about with the restriction against static members of local or
inner classes than I would without it, so I am glad they chose as they did.

--
Lew