Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > inner class, explicit outer class method call

Reply
Thread Tools

inner class, explicit outer class method call

 
 
Yamin
Guest
Posts: n/a
 
      10-22-2004
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
 
Reply With Quote
 
 
 
 
Yogo
Guest
Posts: n/a
 
      10-22-2004
"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


 
Reply With Quote
 
 
 
 
Paul Tomblin
Guest
Posts: n/a
 
      10-22-2004
In a previous article, http://www.velocityreviews.com/forums/(E-Mail Removed) (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 <(E-Mail Removed)> 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
 
Reply With Quote
 
Oscar kind
Guest
Posts: n/a
 
      10-22-2004
Yamin <(E-Mail Removed)> 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
 
Reply With Quote
 
Yamin
Guest
Posts: n/a
 
      10-24-2004
Oscar kind <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access to private data type of an outer class from inner class agentprog@gmail.com C++ 2 04-02-2013 04:48 PM
How to get outer class name from an inner class? John Gordon Python 5 05-09-2012 10:30 AM
Is it standard that inner class inherits the outer class' friendship? Qi C++ 4 05-01-2011 04:57 AM
why inner class can use outer class function ? Ahan Hsieh Java 2 10-05-2007 09:03 AM
Access outer / inner class variables query lonelyplanet999 Java 3 11-18-2003 07:16 PM



Advertisments