Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why inner classes can not have static members?

Reply
Thread Tools

Why inner classes can not have static members?

 
 
bearice@gmail.com
Guest
Posts: n/a
 
      02-04-2008
as title.
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-04-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Markus Tazl
Guest
Posts: n/a
 
      02-04-2008
On 4 Feb., 15:15, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> as title.


Wow, a question in shorthand ?

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

best regards
Markus Tazl
 
Reply With Quote
 
bearice@gmail.com
Guest
Posts: n/a
 
      02-04-2008
On Feb 4, 11:07 pm, Lew <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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


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.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-05-2008
(E-Mail Removed) 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.2. ...
> 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
 
Reply With Quote
 
Guest
Posts: n/a
 
      02-05-2008
>>(E-Mail Removed) wrote:
>Why inner classes can not have static members?


Lew <(E-Mail Removed)> 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 (E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-05-2008
(E-Mail Removed) wrote:
>>> (E-Mail Removed) wrote:

>> Why inner classes can not have static members?

>
> Lew <(E-Mail Removed)> 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 (E-Mail Removed) <http://www.dagon.net/>



--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-05-2008
(E-Mail Removed) wrote:
>>>> Why inner classes can not have static members?


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


(E-Mail Removed) 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-09-2008
On Mon, 4 Feb 2008 07:18:08 -0800 (PST), Markus Tazl <(E-Mail Removed)>
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/tutor...OO/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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why can nsmc, local classes or anonymous classes have static members? Rit Java 23 01-03-2010 05:54 PM
Why inner class can not have static data, static fields or nested class? Robin Java 0 06-06-2007 11:16 AM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments