Why inner classes can not have static members?

Discussion in 'Java' started by bearice@gmail.com, Feb 4, 2008.

  1. Guest

    as title.
    , Feb 4, 2008
    #1
    1. Advertising

  2. Lew Guest

    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
    Lew, Feb 4, 2008
    #2
    1. Advertising

  3. Markus Tazl Guest

    Markus Tazl, Feb 4, 2008
    #3
  4. Guest

    On Feb 4, 11:07 pm, Lew <> wrote:
    > 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.
    , Feb 4, 2008
    #4
  5. Lew Guest

    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
    Lew, Feb 5, 2008
    #5
  6. Guest Guest

    >> wrote:
    >Why inner classes can not have static members?


    Lew <> 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 <http://www.dagon.net/>
    Guest, Feb 5, 2008
    #6
  7. Daniel Pitts Guest

    wrote:
    >>> wrote:

    >> Why inner classes can not have static members?

    >
    > Lew <> 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 <http://www.dagon.net/>



    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Feb 5, 2008
    #7
  8. Lew Guest

    wrote:
    >>>> Why inner classes can not have static members?


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


    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
    Lew, Feb 5, 2008
    #8
  9. Roedy Green Guest

    On Mon, 4 Feb 2008 07:18:08 -0800 (PST), Markus Tazl <>
    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
    Roedy Green, Feb 9, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    6
    Views:
    522
    James Brady
    Dec 4, 2003
  2. Carlo v. Dango
    Replies:
    14
    Views:
    1,027
    Alex Martelli
    Oct 19, 2003
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,828
    Smokey Grindel
    Dec 2, 2006
  4. Robin
    Replies:
    0
    Views:
    415
    Robin
    Jun 6, 2007
  5. Rit
    Replies:
    23
    Views:
    939
    Mike Schilling
    Jan 3, 2010
Loading...

Share This Page