473,394 Members | 1,734 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,394 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 16700
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.