Referencing enclosing class from inner class

J

Jonathan

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();
}

}
 
R

Robert Klemme

Jonathan said:
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
 
J

Jono

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 said:
Jonathan said:
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
 
J

Jono

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
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 said:
Jonathan said:
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
 

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

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top