query regarding static metods

Discussion in 'Java' started by srikanthyellanki, Mar 14, 2007.

  1. why static methods of outer class cannot create the object of non-
    static inner class?
     
    srikanthyellanki, Mar 14, 2007
    #1
    1. Advertisements

  2. srikanthyellanki

    Chris Dollin Guest

    Because they don't have access to an instance for the inner
    object to point to?
     
    Chris Dollin, Mar 14, 2007
    #2
    1. Advertisements

  3. srikanthyellanki

    Lew Guest

    That is correct!

    -- Lew
     
    Lew, Mar 14, 2007
    #3
  4. srikanthyellanki

    Chris Uppal Guest

    They can, but you have to pass an instance of the class to every constructor
    used in that way. The syntax for doing that is wierd (and pointlessly so, IMO)
    but is well-defined: E.g.

    ================
    class Outer
    {
    class Inner
    {
    Inner(int param)
    {
    }
    }

    static void
    test()
    {
    Outer instance = new Outer();
    Inner inner = instance.new Inner(42);
    }
    }
    ================

    -- chris
     
    Chris Uppal, Mar 14, 2007
    #4
  5. srikanthyellanki

    Daniel Pitts Guest

    Funny, I wrote a blog (internal to my work place) about this exact
    syntax.

    I also wrote that I would avoid it at all costs.

    The syntax makes sense if you think about the order symbols are
    resolved. Eventually, the compiler will look for an implied "this.",
    which would lead to "this.new Inner(42)";

    Okay, maybe its weird. but would you rather use "new
    instance.Inner(32)"?
     
    Daniel Pitts, Mar 14, 2007
    #5
  6. srikanthyellanki

    Chris Uppal Guest

    Hmm... Yes, I see your point.

    I'd rather the code looked as if it were doing what it is /acutally/ doing. So
    the inner class's constructor took an /explicit/ instance of the enclosing
    class as its first parameter.

    -- chris
     
    Chris Uppal, Mar 15, 2007
    #6
  7. Then the usual case (using the implied "this") would require an explicit
    "this".

    And would you also require the inner class constructors to declare the
    enclosing instance as the first parameter? If so, they're forced to declare
    a parameter that at least 90% would never use; if not, there's a mismatch
    between the new-expression and the constructor it invokes.
     
    Mike Schilling, Mar 15, 2007
    #7
  8. srikanthyellanki

    Daniel Pitts Guest

    So, you'd prefer this:
    class Outer {
    class Inner {
    }

    Inner getInner() {
    return new Inner(this);
    }
    }

    Hmm, thats broken in more ways than one...

    how about this:

    class Outer {
    static class Inner {
    final Outer outer;
    Inner(Outer outer) {
    this.outer = outer;
    }
    }
    Inner getInner() {
    return new Inner(this);
    }
    }


    I actually do like the static version a bit better if the classes
    COULD be decoupled eventually. However, if you have two coupled
    classes, and one is exclusively created inside the other, why not take
    advantage of the implicit Outer.this.
     
    Daniel Pitts, Mar 15, 2007
    #8
  9. srikanthyellanki

    Chris Uppal Guest

    Daniel Pitts wrote:

    [me:]
    I don't see how it can be "broken" when that's what is actually happening
    today.

    I have no objection to the implicit reference to the outer object. What
    doesn't sit well with me is the implicit /establishment/ of that reference. It
    doesn't seem that the "pretty" syntax (which tries to hide the connection) is
    any prettier than the "raw" alternative -- there's not a lot of point in
    syntactic sugar that isn't even sweet...

    -- chris
     
    Chris Uppal, Mar 16, 2007
    #9
  10. srikanthyellanki

    Daniel Pitts Guest

    I think the effect is mostly intuitive, which allows people to write
    functioning code without worrying about little things like "Wait, how
    come I can access my outer classes members?"

    Well, I don't know. I think that Java should have syntax sugar for
    Runnable and/or Callable.

    SwingUtilities.invokeLater(run{doThings();});
    vs
    SwingUtilities.invokeLater(new Runnable() { public void run()
    {doThings();}});
     
    Daniel Pitts, Mar 16, 2007
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.