Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why inner classes can not have static members?

0 views
Skip to first unread message

bea...@gmail.com

unread,
Feb 4, 2008, 9:15:12 AM2/4/08
to
as title.

Lew

unread,
Feb 4, 2008, 10:07:41 AM2/4/08
to
bea...@gmail.com wrote:
> 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?

--
Lew

Message has been deleted

bea...@gmail.com

unread,
Feb 4, 2008, 3:53:16 PM2/4/08
to
On Feb 4, 11:07 pm, Lew <l...@lewscanon.com> wrote:

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.

Lew

unread,
Feb 4, 2008, 7:38:17 PM2/4/08
to
bea...@gmail.com wrote:
> 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.

<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3>
> 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.

--
Lew

da...@dagon.net

unread,
Feb 4, 2008, 8:01:57 PM2/4/08
to
>>bea...@gmail.com wrote:
>Why inner classes can not have static members?

Lew <l...@lewscanon.com> wrote:
>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.
--
Mark Rafn da...@dagon.net <http://www.dagon.net/>

Daniel Pitts

unread,
Feb 4, 2008, 8:57:35 PM2/4/08
to
da...@dagon.net wrote:
>>> bea...@gmail.com wrote:
>> Why inner classes can not have static members?
>
> Lew <l...@lewscanon.com> wrote:
>> 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;
> }
> }
> }
I actually believe that's called a nested class and has different
semantics.

>
> 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.
> --
> Mark Rafn da...@dagon.net <http://www.dagon.net/>


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Lew

unread,
Feb 4, 2008, 10:19:44 PM2/4/08
to Daniel Pitts
bea...@gmail.com wrote:
>>>> Why inner classes can not have static members?

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

da...@dagon.net wrote:
>> 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 Pitts wrote:
> 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.

--
Lew

Roedy Green

unread,
Feb 8, 2008, 10:02:45 PM2/8/08
to
On Mon, 4 Feb 2008 07:18:08 -0800 (PST), Markus Tazl <m_t...@yahoo.de>
wrote, quoted or indirectly quoted someone who said :

>Anyway, Sun's Docs are your friend. Explained here in detail :
>http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html, see
>Topic : "Inner classes" at the end of the page

I read that several times, and I still don't see why inner classes
could not have private statics. It still seems an arbitrary
restriction. I don't understand the implementation difficulty.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

0 new messages