inner class, explicit outer class method call

Discussion in 'Java' started by Yamin, Oct 22, 2004.

  1. Yamin

    Yamin Guest

    Hey all,

    This is purely an academic question. I know the easy work around for
    it, but I'm just wondering.

    I want to know if there's an easy way to explicitly call an outer
    classes method from an inner class.

    For example
    class x
    {
    public String getName(){ return "xName";}

    class y
    {
    public String getName { return "yName";}
    public String getFullName( return x.getName()+ getName;}
    }
    }

    The getFullName should return "xNameyName";
    The quesiton is how do your specify x.getName() without getName()
    being static.

    The easy way to solve this is to simply pass in the outer class as a
    parameter to the inner class. Then to call x.getName, you'd simply
    use the reference you passed in.

    For example
    class x
    {
    public String getName(){ return "xName";}

    class y
    {
    private x myx;
    public y(x myx) { this.myx = myx};
    public String getName { return "yName";}
    public String getFullName( return myx.getName()+ getName;}
    }
    }


    I guess what's I'm asking, is there an equivalent way to address the
    outerclass in a similar manner as when you inherit from a baseclass,
    you can explicitly call the parent classes member by super.X...is
    there something along the lines of an outer.X. I don't think there
    is, but just seeing what's out there.

    Thanks,

    Yamin
     
    Yamin, Oct 22, 2004
    #1
    1. Advertising

  2. Yamin

    Yogo Guest

    "Yamin" wrote:
    >
    > For example
    > class x
    > {
    > public String getName(){ return "xName";}
    >
    > class y
    > {
    > public String getName { return "yName";}
    > public String getFullName( return x.getName()+ getName;}
    > }
    > }
    >
    > The getFullName should return "xNameyName";
    > The quesiton is how do your specify x.getName() without getName()
    > being static.
    >


    Use this -> x.this.getName()

    The following will then return "xNameyName":

    new x().new y().getFullName();


    Yogo
     
    Yogo, Oct 22, 2004
    #2
    1. Advertising

  3. Yamin

    Paul Tomblin Guest

    In a previous article, (Yamin) said:
    >I guess what's I'm asking, is there an equivalent way to address the
    >outerclass in a similar manner as when you inherit from a baseclass,
    >you can explicitly call the parent classes member by super.X...is
    >there something along the lines of an outer.X. I don't think there
    >is, but just seeing what's out there.


    Try
    x.this.getName()


    --
    Paul Tomblin <> http://xcski.com/blogs/pt/
    Or, to put it another way, if you see a long line of rats streaming off of
    a ship, the correct assumption is *not* "gosh, I bet that's a real nice
    boat now that those rats are gone". - Mike Sphar
     
    Paul Tomblin, Oct 22, 2004
    #3
  4. Yamin

    Oscar kind Guest

    Yamin <> wrote:
    > This is purely an academic question. I know the easy work around for
    > it, but I'm just wondering.
    >
    > I want to know if there's an easy way to explicitly call an outer
    > classes method from an inner class.


    There is no need to work around it: the methods of the outer class are
    available in the inner class. You can just call them.

    It becomes "interesting" when you've hidden the method from the outer
    class by declaring a method woth the same name in the inner class. But
    even then you can reach it using OuterClass.this.method().

    It even works with more than methods, but I haven't used if for anything
    other than fields yet.


    > I guess what's I'm asking, is there an equivalent way to address the
    > outerclass in a similar manner as when you inherit from a baseclass,
    > you can explicitly call the parent classes member by super.X...is
    > there something along the lines of an outer.X. I don't think there
    > is, but just seeing what's out there.


    There is (see above): use OuterClass.this instead of super


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
     
    Oscar kind, Oct 22, 2004
    #4
  5. Yamin

    Yamin Guest

    Oscar kind <> wrote in message
    > There is no need to work around it: the methods of the outer class are
    > available in the inner class. You can just call them.
    >
    > It becomes "interesting" when you've hidden the method from the outer
    > class by declaring a method woth the same name in the inner class. But
    > even then you can reach it using OuterClass.this.method().
    >
    > It even works with more than methods, but I haven't used if for anything
    > other than fields yet.
    >
    >


    Thanks all,

    Learn a new thing everyday :)
     
    Yamin, Oct 24, 2004
    #5
    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. lonelyplanet999

    Access outer / inner class variables query

    lonelyplanet999, Nov 11, 2003, in forum: Java
    Replies:
    3
    Views:
    5,340
    lonelyplanet999
    Nov 18, 2003
  2. Ahan Hsieh
    Replies:
    2
    Views:
    594
    Ahan Hsieh
    Oct 5, 2007
  3. Qi
    Replies:
    4
    Views:
    795
  4. John Gordon
    Replies:
    5
    Views:
    466
    Adam Skutt
    May 9, 2012
  5. Replies:
    2
    Views:
    256
Loading...

Share This Page