Why inner classes can not have static members?

L

Lew

as title.

Cultural note: It's considered polite to repeat the question inside the body
of the post to facilitate others' ability to read the conversation.

Inner classes can have static members, provided such members are compile-time
constants.

Asking "why" the language specifies something is an exercise in telepathy,
unless the language designers left some notes or blog entries or white papers
behind explaining the rational, which they might have done. GIYF.

I know that static members of inner classes would confuse me. Would such a
member only be static within the context of the immediately enclosing instance
of some instances of the inner class, or would it apply to all instances of
the inner class?

The problem is that inner classes are (generally) instantiated within an
instance of their enclosing class. Inner classes are not "static" enough on
their own for me to be comfortable with a "static" that operates across all
enclosing instance contexts. I suppose I could get used to it if Java were
defined that way, but it isn't. It's defined to make inner classes very
dependent on their enclosing instances.

If you want static members, use non-inner nested classes, or avoid nested
classes altogether.

Why is a static member of an inner class necessary for you? Why is an
alternative idiom not acceptable?
 
B

bearice

Cultural note: It's considered polite to repeat the question inside the body
of the post to facilitate others' ability to read the conversation.

Inner classes can have static members, provided such members are compile-time
constants.

Asking "why" the language specifies something is an exercise in telepathy,
unless the language designers left some notes or blog entries or white papers
behind explaining the rational, which they might have done. GIYF.

I know that static members of inner classes would confuse me. Would such a
member only be static within the context of the immediately enclosing instance
of some instances of the inner class, or would it apply to all instances of
the inner class?

The problem is that inner classes are (generally) instantiated within an
instance of their enclosing class. Inner classes are not "static" enough on
their own for me to be comfortable with a "static" that operates across all
enclosing instance contexts. I suppose I could get used to it if Java were
defined that way, but it isn't. It's defined to make inner classes very
dependent on their enclosing instances.

If you want static members, use non-inner nested classes, or avoid nested
classes altogether.

Why is a static member of an inner class necessary for you? Why is an
alternative idiom not acceptable?

I see, sorry for my impolite way of asking. And thank all of you for
answering me.
I'm not really need a static member in an inner class. just very
curious about it when the complier tells me it is an error.
Thanks again.
 
L

Lew

I'm not really need a static member in an inner class. just very
curious about it when the complier tells me it is an error.

Well, the simple answer is because that's the way it's defined for the
language. The rules explicitly allow only compile-time constants to be static
members of inner classes. Non-inner nested classes do not have this restriction.

Inner classes may not declare static initializers (§8.7) or member interfaces.
Inner classes may not declare static members,
unless they are compile-time constant fields (§15.28). ...
Nested classes that are not inner classes may declare static members freely,
in accordance with the usual rules of the Java programming language.

In practice the restriction causes no difficulties. If anything, it makes
inner classes easier to deal with, as one can conceptualize the inner class as
"belonging" entirely to the enclosing instances that instantiate it without
wondering if static members should differ between different enclosing instances.

Language designers always face interesting decisions like this, such as what
the value of the remainder operator '%' should be when the denominator is
negative. Sometimes they get it wrong, as many feel Java did by not (yet)
making generics reifiable. I have never encountered an authoritative
explanation for why inner classes in Java cannot have
non-compile-time-constant static members; OTOH I've never encountered a
situation where the restriction caused trouble.

Inner classes are an odd duck anyway. They're useful for sure, because having
a class than can access the members (even private ones) of its enclosing class
instance sure helps out - it mitigates the lack of closures, for example.
However, one should always consider using a non-inner nested class or
non-public top-level class before creating an inner class that does not need
such access.
 
G

Guest

Why inner classes can not have static members?

Lew said:
Inner classes can have static members, provided such members are compile-time
constants.

Static inner classes can have static members without such a restriction. This
is legal:

class Test {
static class Foo {
static int a;
public static void setA(int newA) {
a = newA;
}
}
}

The difference between a static and non-static inner class is important. A
static inner class is a fairly normal class, and behaves very similarly to
other outer classes. A non-static inner class has an implicit pointer to an
instance of it's enclosing class, so doesn't really have a static context to
execute in.

Note: anonymous inner classes are always non-static.
 
D

Daniel Pitts

Static inner classes can have static members without such a restriction. This
is legal:

class Test {
static class Foo {
static int a;
public static void setA(int newA) {
a = newA;
}
}
}
I actually believe that's called a nested class and has different
semantics.
 
L

Lew

Static inner classes [sic] can have static members without such a
restriction. This
is legal:

class Test {
static class Foo { // *not* an inner class
static int a;
public static void setA(int newA) {
a = newA;
}
}
}

Daniel said:
I actually believe that's called a nested class and has different
semantics.

"Static inner class" is a contradiction. Inner classes are never static, in
that they are *defined* as nested classes that do not use the "static" keyword.

Nested classes comprise both inner classes and static nested classes.

The relevant section of the JLS is linked upthread.
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top