By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

inner class referencing outer classes members

P: n/a
i have an inner class and an outer class, both of which have an
identically named member. how from the inner class can i reference the
outer member? (the example code should illustrate this problem).

class Outer
{
int x;
class Inner
{
int x;
void setOuterX(int y)
{
// this.this.x = y;
}
}
}

the commented line gives you the idea, but i know that doesn't work (i
tried it!)...

i'm sure this is simple enough, can someone help me out?

thanks much!

murat

--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Murat Tasan wrote:
i have an inner class and an outer class, both of which have an
identically named member. how from the inner class can i reference the
outer member? (the example code should illustrate this problem).

class Outer
{
int x;
class Inner
{
int x;
void setOuterX(int y)
{
// this.this.x = y;
}
}
}

the commented line gives you the idea, but i know that doesn't work (i
tried it!)...

i'm sure this is simple enough, can someone help me out?


The simplest thing to do is to rename the inner variable. This would be
good style as well, so that the next guy who needs to modify your class
isn't taking your name in vain while he does it. And don't be so
certain that guy wouldn't be you... :)

Ray

P.S. I think there is a way to do it, without renaming anything or
adding methods (which, by the way, is how the compiler actually has the
inner class access the outer class's variables), but I think it is
better if you don't know.

Jul 17 '05 #2

P: n/a
thanks for nothing, really... but:
i found it anyhow.
in section 15.8.4 in the java languages specs, for anyone who's
interested...

it's called a "qualified this"

you can identify which class the "this" refers to by placing the class
name in front of it.

a runtime exception is thrown if the class name is not an encapsulating
class of the class that is accessing the member.

later,

murat

On Wed, 29 Oct 2003, Raymond DeCampo wrote:
Murat Tasan wrote:
i have an inner class and an outer class, both of which have an
identically named member. how from the inner class can i reference the
outer member? (the example code should illustrate this problem).

class Outer
{
int x;
class Inner
{
int x;
void setOuterX(int y)
{
// this.this.x = y;
}
}
}

the commented line gives you the idea, but i know that doesn't work (i
tried it!)...

i'm sure this is simple enough, can someone help me out?


The simplest thing to do is to rename the inner variable. This would be
good style as well, so that the next guy who needs to modify your class
isn't taking your name in vain while he does it. And don't be so
certain that guy wouldn't be you... :)

Ray

P.S. I think there is a way to do it, without renaming anything or
adding methods (which, by the way, is how the compiler actually has the
inner class access the outer class's variables), but I think it is
better if you don't know.


--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.