Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Referencing enclosing class from inner class

Thread view: 
Jonathan - 31 Jul 2006 12:34 GMT
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 - 31 Jul 2006 12:48 GMT
> Hi everyone,
> I'm trying to understand inner classes, but came across the following
[quoted text clipped - 23 lines]
>             System.out.println(getValue()); //<-- inner
>             System.out.println(getText()); //<-- enclosing

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

>         }
>     }
[quoted text clipped - 17 lines]
>    
> }

Will print

200
hello!
100

Kind regards

    robert
Jono - 31 Jul 2006 13:16 GMT
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!

> > Hi everyone,
> > I'm trying to understand inner classes, but came across the following
[quoted text clipped - 58 lines]
>
>     robert
Jono - 31 Jul 2006 13:30 GMT
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!
[quoted text clipped - 61 lines]
> >
> >     robert


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.