Why can nsmc, local classes or anonymous classes have static members?

R

Rit

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
 
A

Arne Vajhøj

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
 
M

markspace

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

Rit

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
 
R

Rit

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
 
A

Arne Vajhøj

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
 
L

Lew

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.
 
M

markspace

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


Except of course for anonymous local classes, which I'm sure you've seen
quite a few of. ;)
 
M

markspace

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 didn't look this up but: I'm pretty sure that while you are correct
that local classes in a static context have no enclosing *object* they
do have an enclosing *class*. That's what I'm referring to, the class
object itself. Not an instantiation of said class object.

Even local classes in a static context have an outer, enclosing class
which can be used for any needed static members, like constants or methods.
 
R

Roedy Green

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

Can some throw light on that ?

Inner classes are not permitted to have static methods or fields. That
is not quite true. They are allowed static final compile time
constants, which are treated as if there were literals. Sorry I don’t
know why the restriction. Nobody I have asked knows why. This is
probably the single most annoying fact about nested classes. If inner
classes need statics, they have to get the outer class to hold them,
or you have to use static nested classes or you have to inherit the
static fields. Oddly, inner classes are permitted to extend classes
that do have static methods and fields.

see http://mindprod.com/jgloss/nestedclasses.html
 
R

Roedy Green

Anonymous classes can not have static methods because those
can not be called by anyone.

You could not call them from outside, since they would not have a
name, but why could you not call such static methods within the
anonymous class?
 
L

Lew

Roedy said:
static fields. Oddly, inner classes are permitted to extend classes
that do have static methods and fields.

That's not so odd considering that static members aren't really inherited,
just accessible.
 
A

Arne Vajhøj

Except of course for anonymous local classes, which I'm sure you've seen
quite a few of. ;)

I have seen plenty of anonymous classes.

Traditionally they are not considered to be local classes.

Arne
 
A

Arne Vajhøj

You could not call them from outside, since they would not have a
name, but why could you not call such static methods within the
anonymous class?

I guess you could do that.

But then what benefits would that static method provide
that a similar non-static method would not provide?

Arne
 
R

Roedy Green

I guess you could do that.

But then what benefits would that static method provide
that a similar non-static method would not provide?

same thing a normal static method does: single copy of variable
common to all instances, persistence, ability to count instances.
 
L

Lew

Arne Vajhøj wrote, quoted or indirectly quoted someone who said :
Roedy said:
same thing a normal static method does:  single copy of variable
common to all instances, persistence, ability to count instances.

Let's see. If it's a local class without an enclosing instance (e.g.,
in a static context), there will not be more instances than are
declared in that invocation of the context. A variable can be shared
by making it a final variable in the context outside the local class.
Counting the instances will be useless.

If it's an inner class with an enclosing instance, do you want the
static member shared across all enclosing instances or only among
invocations within the same enclosing instance? How "static" should
'static' be?

It just doesn't make sense to have static members in inner classes.
 
M

Mike Schilling

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

No, it wouldn't. You can dicuss it all you like, but it's quite clear that
the only sensible decision is one copy of each static per Class instance.
(It might be fuzzy if the msmc and its parent class could be loaded by
different classloaders, but that's not possible.)
 
A

Arne Vajhøj

No, it wouldn't. You can dicuss it all you like, but it's quite clear that
the only sensible decision is one copy of each static per Class instance.
(It might be fuzzy if the msmc and its parent class could be loaded by
different classloaders, but that's not possible.)

I am not sure that I would call that sensible.

It is not exactly "classic static behavior".

Arne
 
A

Arne Vajhøj

same thing a normal static method does: single copy of variable
common to all instances, persistence, ability to count instances.

But the static method can not be called from a static context,
so whatever it does could be achieved by making it non static.

And regarding counting instances, then look at what Mike Schilling
considers sensible.

Arne
 
M

Mike Schilling

Arne said:
I am not sure that I would call that sensible.

It is not exactly "classic static behavior".

It seems to me that it is. "static" means "one per Class". not "one
per instance".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top