Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local class

 
Tanuja Vaid
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to KM on pg 225,local class(static) can access static members in enclosing context+local final variables.
I tried this code it compiles fine.If the above is correct then how am i able to access double y and int w.
Can somebody please clarify.
class Gunda{
static int i;
int w;
}
class NonStatic extends Gunda{
private static int x;
protected double y;
public static void Juno(final int m){
final int j=10;
int k=5;
class C{
int jj=j;
int mm=m;
int x=10;
double y=5;
int i;
int w;
}
}
}
TIA
Tanuja
 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanuja,
In ur code where are u trying to access non-static variables from the encosling class..
what u r doing is that u r declaring double d and int w ; as nonstatic local variables inside the local class...so this compiles fine..
modify the code like this and see the result---
public static void Juno(final int m){
final int j=10;
int k=5;
class C{
int jj=j;
int mm=m;
int x=10;
double y=NonStatic.this.y;//Compiler error
int i;
int w=NonStatic.this.w;///Compiler error.
}
Swapna
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is that because y and w are not static???
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanuja,
Not sure what you wanted, but if you wanted to access w and y, you can use the following.
System.out.println("y=" + new NonStatic().y);
System.out.println("w=" + new Gunda().w);
Hope it helps anyway....
Clement
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic