473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

inner class referencing outer classes members

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
2 16701
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: devu | last post by:
Currently I'm working on a class that performs a batch process as per scheduling for all employees in a company. Being a batch process load is obviously a huge factor. The process first needs to...
7
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
3
by: Glen Able | last post by:
class Outer { typedef int MyInt; class Inner { Outer::MyInt m_thing; }; };
3
by: trialproduct2004 | last post by:
hi all i am having application which is using two classes. in one class i am creating instance of another class and calling method of that class. what i want is from inner class's method i want...
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
5
by: Martijn Mulder | last post by:
A construction like this: class Outer { class Inner:Outer { } } compiles without problem but does it introduce infinity?
1
by: mrstephengross | last post by:
I'm making progress on mixing templates with friends (sounds like a drinking game, huh?). Anyway, here's the situation. I've got an "Outer" class with a private "Inner" class (sub-class,...
9
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
3
by: ChrisW | last post by:
Hiya, So I have a class that creates threads within it. These threads are a class underneath the parent class. I want to access values in the parent class from the threads while they run. Yet...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.