Referencing enclosing class from inner class

Discussion in 'Java' started by Jonathan, Jul 31, 2006.

  1. Jonathan

    Jonathan Guest

    Hi everyone,
    I'm trying to understand inner classes, but came across the following
    problem. If I define two types (one is an inner class), and each type
    has a method with the same name, is there a way that I can call the
    enclosing instance's method from within an instance of the inner class?
    I'm guessing that (as per the example pasted below) the getValue()
    methods aren't overloaded or overridden because the two classes don't
    participate in super-class/sub-class OO relationship. I'd be grateful
    for any insight.
    Thanks,
    Jonathan

    public class EnclosingClass {

    private int _value = 100;

    private class InnerClass
    {
    public int getValue()
    {
    return _value * 2;
    }

    public void println()
    {
    System.out.println(getValue()); //<-- inner
    System.out.println(getText()); //<-- enclosing
    }
    }

    public int getValue()
    {
    return _value;
    }

    public String getText()
    {
    return "hello!";
    }

    public static void main(String[] args)
    {
    // create an instance of both classes and call the println method of
    the inner class
    new EnclosingClass().new InnerClass().println();
    }

    }
     
    Jonathan, Jul 31, 2006
    #1
    1. Advertising

  2. Jonathan wrote:
    > Hi everyone,
    > I'm trying to understand inner classes, but came across the following
    > problem. If I define two types (one is an inner class), and each type
    > has a method with the same name, is there a way that I can call the
    > enclosing instance's method from within an instance of the inner class?
    > I'm guessing that (as per the example pasted below) the getValue()
    > methods aren't overloaded or overridden because the two classes don't
    > participate in super-class/sub-class OO relationship. I'd be grateful
    > for any insight.
    > Thanks,
    > Jonathan
    >
    > public class EnclosingClass {
    >
    > private int _value = 100;
    >
    > private class InnerClass
    > {
    > public int getValue()
    > {
    > return _value * 2;
    > }
    >
    > public void println()
    > {
    > System.out.println(getValue()); //<-- inner
    > System.out.println(getText()); //<-- enclosing


    System.out.println( EnclosingClass.this.getValue() ); //
    <-- enclosing

    > }
    > }
    >
    > public int getValue()
    > {
    > return _value;
    > }
    >
    > public String getText()
    > {
    > return "hello!";
    > }
    >
    > public static void main(String[] args)
    > {
    > // create an instance of both classes and call the println method of
    > the inner class
    > new EnclosingClass().new InnerClass().println();
    > }
    >
    > }
    >


    Will print

    200
    hello!
    100

    Kind regards

    robert
     
    Robert Klemme, Jul 31, 2006
    #2
    1. Advertising

  3. Jonathan

    Jono Guest

    Thank you. It's a bit of a weird syntax, and NetBeans' "intellisense"
    doesn't offer it up as an option, but it compiles and runs fine!

    Robert Klemme wrote:

    > Jonathan wrote:
    > > Hi everyone,
    > > I'm trying to understand inner classes, but came across the following
    > > problem. If I define two types (one is an inner class), and each type
    > > has a method with the same name, is there a way that I can call the
    > > enclosing instance's method from within an instance of the inner class?
    > > I'm guessing that (as per the example pasted below) the getValue()
    > > methods aren't overloaded or overridden because the two classes don't
    > > participate in super-class/sub-class OO relationship. I'd be grateful
    > > for any insight.
    > > Thanks,
    > > Jonathan
    > >
    > > public class EnclosingClass {
    > >
    > > private int _value = 100;
    > >
    > > private class InnerClass
    > > {
    > > public int getValue()
    > > {
    > > return _value * 2;
    > > }
    > >
    > > public void println()
    > > {
    > > System.out.println(getValue()); //<-- inner
    > > System.out.println(getText()); //<-- enclosing

    >
    > System.out.println( EnclosingClass.this.getValue() ); //
    > <-- enclosing
    >
    > > }
    > > }
    > >
    > > public int getValue()
    > > {
    > > return _value;
    > > }
    > >
    > > public String getText()
    > > {
    > > return "hello!";
    > > }
    > >
    > > public static void main(String[] args)
    > > {
    > > // create an instance of both classes and call the println method of
    > > the inner class
    > > new EnclosingClass().new InnerClass().println();
    > > }
    > >
    > > }
    > >

    >
    > Will print
    >
    > 200
    > hello!
    > 100
    >
    > Kind regards
    >
    > robert
     
    Jono, Jul 31, 2006
    #3
  4. Jonathan

    Jono Guest

    Upon further investigation I've found that the reference you described
    is called a "qualified this" so I've been able to read up more about it
    in the JLS. Thank you.
    Jono

    Jono wrote:

    > Thank you. It's a bit of a weird syntax, and NetBeans' "intellisense"
    > doesn't offer it up as an option, but it compiles and runs fine!
    >
    > Robert Klemme wrote:
    >
    > > Jonathan wrote:
    > > > Hi everyone,
    > > > I'm trying to understand inner classes, but came across the following
    > > > problem. If I define two types (one is an inner class), and each type
    > > > has a method with the same name, is there a way that I can call the
    > > > enclosing instance's method from within an instance of the inner class?
    > > > I'm guessing that (as per the example pasted below) the getValue()
    > > > methods aren't overloaded or overridden because the two classes don't
    > > > participate in super-class/sub-class OO relationship. I'd be grateful
    > > > for any insight.
    > > > Thanks,
    > > > Jonathan
    > > >
    > > > public class EnclosingClass {
    > > >
    > > > private int _value = 100;
    > > >
    > > > private class InnerClass
    > > > {
    > > > public int getValue()
    > > > {
    > > > return _value * 2;
    > > > }
    > > >
    > > > public void println()
    > > > {
    > > > System.out.println(getValue()); //<-- inner
    > > > System.out.println(getText()); //<-- enclosing

    > >
    > > System.out.println( EnclosingClass.this.getValue() ); //
    > > <-- enclosing
    > >
    > > > }
    > > > }
    > > >
    > > > public int getValue()
    > > > {
    > > > return _value;
    > > > }
    > > >
    > > > public String getText()
    > > > {
    > > > return "hello!";
    > > > }
    > > >
    > > > public static void main(String[] args)
    > > > {
    > > > // create an instance of both classes and call the println method of
    > > > the inner class
    > > > new EnclosingClass().new InnerClass().println();
    > > > }
    > > >
    > > > }
    > > >

    > >
    > > Will print
    > >
    > > 200
    > > hello!
    > > 100
    > >
    > > Kind regards
    > >
    > > robert
     
    Jono, Jul 31, 2006
    #4
    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. Tor Iver Wilhelmsen
    Replies:
    0
    Views:
    436
    Tor Iver Wilhelmsen
    Jul 18, 2003
  2. Jayaram
    Replies:
    0
    Views:
    456
    Jayaram
    Jul 18, 2003
  3. Stanimir Stamenkov
    Replies:
    4
    Views:
    518
    Chris Uppal
    Jul 14, 2006
  4. bugbear
    Replies:
    4
    Views:
    468
    bugbear
    Aug 23, 2006
  5. Mike Schilling

    Re: Referencing enclosing instance

    Mike Schilling, Apr 28, 2010, in forum: Java
    Replies:
    7
    Views:
    556
    Daniel Pitts
    Apr 30, 2010
Loading...

Share This Page