i have another question for final of anonymous class.
Inside an anonymous class, access the attributes and methods of class where the anonymous class is defined.
Access local variables of method where the anonymous class is defined provided they are final. This is because local variable would no longer exists when the method is finished.
package a;
public class A {
private int i = 4;
public void meth() {
System.out.println("will not use");
}
public void meth2() {
int j = 4;
final int k = 3;
A a = new A() {
public void meth() {
System.out.println("i-4 is " + (i - 4));
System.out.println("j-4 is " + (j - 4));
System.out.println("k-4 is " + (k - 4));
}
};
a.meth();
}
public static void main(String st[]) {
A a = new A();
a.meth2();
}
}
the following result: run: i-4 is 0 j-4 is 0 k-4 is -1
at the upper example (i dont get any error and with result) ........i can access all variable and method ??? but why it say only can final and the definition in the sub class, the variable will no longer exists without final.