• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In an innerclass, how to access the final variable of the enclosing method

 
venkata nagarjuna
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
How to access the method_inner variable "s" inside its enclosing class inner's method "display()" so that to get the output as "hi2"?

class outer
{
String s="hi1";
void method_inner()
{
final String s="hi2";
class inner
{
String s="hi3";
void display()
{
String s="hi4";
System.out.println("outer variable "+outer.this.s);
System.out.println("inside the method_class: "+this.s);
System.out.println("inside the method class method var"+ s);

}
}
inner inobj=new inner();
inobj.display();
}

public static void main(String args[])
{
outer obj=new outer();
obj.method_inner();
}
}

[ April 29, 2008: Message edited by: venkata nagarjuna ]
[ April 29, 2008: Message edited by: venkata nagarjuna ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class outer
{
String s="hi1";
void method_inner()
{
final String s1="hi2";
class inner
{
final String s="hi3";
void display()
{
String s="hi4";
System.out.println("outer variable: "+ outer.this.s);
System.out.println("inside the method_class: "+inner.this.s);
System.out.println("final variable: "+ s1 );
System.out.println("value of s: "+s);
}
}
inner inobj=new inner();
inobj.display();
}

public static void main(String args[])
{
outer obj=new outer();
obj.method_inner();
}
}

output...
outer variable: hi1
inside the method_class: hi3
final variable: hi2
value of s: hi4

Correct me if i am wrong..
[ April 29, 2008: Message edited by: Dinesh Tahiliani ]
 
venkata nagarjuna
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi dinesh,

If it is another variable then it is ok to access that variable. But if the variable has same name as of the inner class instance variable then how to access that method variable from inside the inner class method.

thanks,
nagarjuna.
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't access it. it is double shadowed. there is no keyword such as this, that could refer to local variable that you want to access. here is simplified example:

you can access s at line 2 only with a call that you made in line 5, but that is impossible since line 2 is shadowed by variables defined in line 4, and line 3. also without call this.s you couldn't access line 3 s because it is shadowed by line 4. so if you want to access line 2 variable it must be accessible i.e. not shadowed or as Dinesh did, you have to rename it.
and please use code tags...
 
venkata nagarjuna
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ivan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic