About Question enthuware.ocpjp.v7.2.1472 :
Posted: Wed Mar 30, 2016 9:07 am
Hi,
I'm a bit confused here.
I know method local variables are supposed to be accessible to classes defined within those methods only if those local variables are final, but then why does this work?
public class AccessTest {
static int x;
private String s;
void foo(String fooArg){
int count = 3;
class inner{
void fooInner(int k){
k = 0;
k = count;
System.out.println(k);
}
}
new inner().fooInner(0);
}
public static void main(String [] args){
new AccessTest().foo("hello");
}
}
Clearly I am able to access count and assign it to k.
Should I be interpreting accessible as able to be modified? Because I know if I try to assign to count then I'll get the error about it not being final.
But if I declare count as final then it can't be modified anyway.
Thanks
I'm a bit confused here.
I know method local variables are supposed to be accessible to classes defined within those methods only if those local variables are final, but then why does this work?
public class AccessTest {
static int x;
private String s;
void foo(String fooArg){
int count = 3;
class inner{
void fooInner(int k){
k = 0;
k = count;
System.out.println(k);
}
}
new inner().fooInner(0);
}
public static void main(String [] args){
new AccessTest().foo("hello");
}
}
Clearly I am able to access count and assign it to k.
Should I be interpreting accessible as able to be modified? Because I know if I try to assign to count then I'll get the error about it not being final.
But if I declare count as final then it can't be modified anyway.
Thanks