• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

inner classes

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a non-static inner class has a member variable that has exactly same name as of its enclosing class then how can we access the member variable of the enclosing class in a method of inner-class?
If we write this.x or simply x then it displays the x of inner class both the times.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
If the enclosing class' member variable, say x, is static then it can be referred to in the inner class' method by enclosingclassname.x
If the enclosing class' member variable x is not static, then guess, its instance must be used to refer to x.
In the following code, both x and y appear in both Outer & Inner. But in Outer, y is static and x is not static. So in inner class, o.x and Outer.y are used to refer to x and y of the Outer class respectively. The variable z appears in Outer only, so it can be referred to as just z in inner class' method.

The output is as follows:
x 20
this.x 20
o.x 10
y 20
Outer.y 10
z 10
Hope this helps,
Gaja.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way I know of is to pass the inner class a reference to the outer class like:
<code>
class OuterClass
{
class InnerClass
{
void method()
{
System.out.println( outer.memberVariable );
}

String memberVariable = "Inner";

public InnerClass( OuterClass outer )
{
this.outer = outer;
}

private OuterClass outer;
}

String memberVariable = "Outer";
public static void main( String[] args )
{
OuterClass outer = new OuterClass();
outer.new InnerClass( outer ).method();

}
}
</code>
This will print out "Outer".

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easy answer is :
enclosingClassName.this.x
 
Guy Reynolds
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that is easier.
 
Ranch Hand
Posts: 2373
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy, very tricky example but without explanation...
------------------
azaman
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!