inner class, explicit outer class method call

Y

Yamin

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
 
Y

Yogo

Yamin said:
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
 
P

Paul Tomblin

In a previous article, (e-mail address 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()
 
O

Oscar kind

Yamin said:
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
 
Y

Yamin

Oscar kind said:
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 :)
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top