aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Local class 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 "Local class" Watch "Local class" New topic
Author

Local class

Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
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

Joined: Jan 18, 2002
Posts: 75
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


SCPJ2
andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
is that because y and w are not static???
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
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


best regards,<br />Steven<br />SCJP, SCEA
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local class