• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Method Local Inner Class

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class MethodLocalInnerClass
{
private int a=10;
public void display()
{
final int a=20; //line 1
System.out.println("This is outer class display() ");

class MethodInner
{
private int a;

public void display()
{
System.out.println("The value of the Inner class a is : "+a);
System.out.println("The value of the Ouuter class a is : "+MethodLocalInnerClass.this.a);
System.out.println("The value of the Local variable in the method is : "+a);
}
}
MethodInner mi=new MethodInner();
mi.display();

}
}


class MethodLocalInnerClassMain
{
public static void main(String... args)
{
MethodLocalInnerClass mlic=new MethodLocalInnerClass();
mlic.display();

}

}



In the above code how to access the variable defined at line1 in the display method.

I have tried ,but i could not get idea ,how to do that.

Thanks
Anil kumar
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,

You have shadowed the variable "a" inside the MethodInner. I don't see whether there is any way to access the variable defined at line #1.

You code is something like this:

 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Chandra

Ya i have shadowed the variable ,i know ,but i am asking is there any way to get that value in the method.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anil kumar:
Hi

Chandra

Ya i have shadowed the variable ,i know ,but i am asking is there any way to get that value in the method.



Nay Anil,

You are only able to access:
1- The enclosing class,
2- innerclass instance variables
Because you have "this" reference.

Topclass.this.varname; (for first case)
this.varname (for second case)
3- When you write simply "a" from the instance method, this can first be local variable defined inside the method or the member variable.

4- If you write "a" in static context (from static method or block),
it can first be local variable as always or the class variable (declared static)

There are no way to access variable that is defined inside a method and
you shadow it inside the methodlocal inner class. You think yourself, how can you reach to that. No way!
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic