Inner/Nested static stuff

Discussion in 'Java' started by -, Jun 14, 2005.

  1. -

    - Guest

    If I refactor the following class into one below it, should i make the
    below nested class static?


    public class A {

    private int hgap = 0;
    private int vgap = 0;

    ...

    public A() {
    }

    ...

    public void setHGap(int hgap) {
    this.hgap = hgap;
    }

    ...
    }



    public class A {

    private Gap gap = new Gap();

    ...

    public A() {
    }

    ...

    public void setGap(Gap gap) {
    this.gap = gap;
    }

    ...

    public class Gap { <---- to be static or not?

    private int hgap = 0;
    private int vgap = 0;

    public Gap() {
    }

    ...

    public void setHGap(int hgap) {
    this.hgap = hgap;
    }

    ...
    }
    }


    I'm guessing it should be static so that i can call 'a.setGap(new
    A.Gap());'.

    If so, under what circumstances should i make it non static and call a
    'a.setGap(a.new Gap());' instead? If i were to do this and the
    constructor accepts a Gap object as one of the parameter, then it's a
    problem since 'a' doesn't exist yet.

    Also, if most of the time nested classes are static, can i simply put it
    in its own class rather than making it nested if the nested class
    does'nt access any of the enclosing class's variables?
    -, Jun 14, 2005
    #1
    1. Advertising

  2. - wrote:

    > If I refactor the following class into one below it, should i make the
    > below nested class static?


    [code elided}

    > I'm guessing it should be static so that i can call 'a.setGap(new
    > A.Gap());'.


    You shouldn't be guessing, and the syntax implied by your choice should
    not be your guiding principle. To answer the question intelligently,
    you need to understand the difference between making a nested class
    static and not.

    If a nested class is not static, then every instance has a containing
    instance of the containing class. The inner object can access the
    containing object's variables and invoke its methods, even the private
    ones. There are other implications as well that we probably do not need
    to go into at the moment.

    If a nested class is static, then it can refer to all its containing
    class' static members, including the private ones, but it is not
    associated with any particular instance of the containing class. As
    access to static members is not usually a big concern, the role of the
    containing class is largely to provide a namespace with finer
    granularity than a package.

    In general, it is usually better to make a nested class static if it
    does not depend on a particular instance.

    > If so, under what circumstances should i make it non static and call a
    > 'a.setGap(a.new Gap());' instead? If i were to do this and the
    > constructor accepts a Gap object as one of the parameter, then it's a
    > problem since 'a' doesn't exist yet.


    You are correct: you cannot pass an inner class instance to the
    constructor of its own containing instance. You can pass parameters by
    which the constructor can create the inner instance, however. If you
    find yourself wanting to do this, though, then you probably have a class
    that should be static.

    > Also, if most of the time nested classes are static, can i simply put it
    > in its own class rather than making it nested if the nested class
    > does'nt access any of the enclosing class's variables?


    Yes, you certainly can. If the class represents something that is
    meaningful in contexts other than the containing class, then it is
    probably better to make it a top-level class. If the nested class
    represents something that is only meaningful in the context of the
    containing class, however, then it makes sense to use a nested class,
    even if it doesn't rely on the containing class' members. This all is a
    matter of style, however.

    --
    John Bollinger
    John C. Bollinger, Jun 14, 2005
    #2
    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. Razvan
    Replies:
    5
    Views:
    11,297
    Dale King
    Jul 27, 2004
  2. Carlo v. Dango
    Replies:
    14
    Views:
    1,025
    Alex Martelli
    Oct 19, 2003
  3. bugbear
    Replies:
    4
    Views:
    434
    bugbear
    Aug 23, 2006
  4. Pyenos
    Replies:
    2
    Views:
    385
    Pyenos
    Dec 27, 2006
  5. Robin
    Replies:
    0
    Views:
    414
    Robin
    Jun 6, 2007
Loading...

Share This Page