Rit said:
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.