Why only final type objects / primitives are permitted in annonymous class?
Thanks in advance.
posted 6 years ago
That only applies to local variables.
The point is that if it's a local variable, it's probably going to be out of scope at the time that the anonymous class code gets executed. The solution Java uses is that a copy of the local variable is taken. But that could lead to confusion if the value of the variable then changes. So they've added the restriction that the variable must be final, which means that you know the value never will change.
Instance variables and static variables will always be in scope for the anonymous class, so there's no problem there.