Referencing enclosing class from inner class  
Author Message
Jonathan





PostPosted: 2006-7-31 19:35:00 Top

java-programmer, Referencing enclosing class from inner class 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();
}

}

 
Robert Klemme





PostPosted: 2006-7-31 19:48:00 Top

java-programmer >> Referencing enclosing class from inner class 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





PostPosted: 2006-7-31 20:17:00 Top

java-programmer >> Referencing enclosing class from inner class 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





PostPosted: 2006-7-31 20:31:00 Top

java-programmer >> Referencing enclosing class from inner class 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