aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes In an innerclass, how to access the final variable of the enclosing method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "In an innerclass, how to access the final variable of the enclosing method" Watch "In an innerclass, how to access the final variable of the enclosing method" New topic
Author

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

venkata nagarjuna
Greenhorn

Joined: Mar 24, 2008
Posts: 7
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

Joined: Aug 06, 2007
Posts: 486
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 ]

Thanks<br />Dinesh
venkata nagarjuna
Greenhorn

Joined: Mar 24, 2008
Posts: 7
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

Joined: Oct 31, 2007
Posts: 100
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...


<a href="http://faq.javaranch.com/java/UseRealWords" target="_blank" rel="nofollow">Use Real Words</a> <a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">!!!Use Code Tags!!!</a> <a href="http://faq.javaranch.com/java/SayThanks" target="_blank" rel="nofollow">Say Thanks</a><br />scjp6
venkata nagarjuna
Greenhorn

Joined: Mar 24, 2008
Posts: 7
thanks ivan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In an innerclass, how to access the final variable of the enclosing method