P
Philipp
Hello,
In JLS 8.1.3, where inner classes are specified, we read "Any local
variable, formal method parameter or exception handler parameter used
but not declared in an inner class must be declared final.".
Why is this needed?
As far as I can see, as a programmer, you can always get around of
this limitation by making a final copy of the variable (see code
below).
public class Test {
public void doSomething(){
String a = "hello";
final String b = a;
Runnable r = new Runnable(){
public void run() {
System.out.println(a); // compile error, but would be OK with
b
}
};
}}
As a side question. Do you generally declare all your local variables
final when this is possible? Is there a substantial gain in doing
this?
Phil
In JLS 8.1.3, where inner classes are specified, we read "Any local
variable, formal method parameter or exception handler parameter used
but not declared in an inner class must be declared final.".
Why is this needed?
As far as I can see, as a programmer, you can always get around of
this limitation by making a final copy of the variable (see code
below).
public class Test {
public void doSomething(){
String a = "hello";
final String b = a;
Runnable r = new Runnable(){
public void run() {
System.out.println(a); // compile error, but would be OK with
b
}
};
}}
As a side question. Do you generally declare all your local variables
final when this is possible? Is there a substantial gain in doing
this?
Phil