12/25/2008

Local inner classes

A class defined in a method is called a local inner class.
The instance of a local inner class should be generated in the same method where the local inner class is defined.
You cannot use access modifiers but can use
final or abstract keyword
Local variables except
final variables cannot be accessed from a local inner class.
Members of an outer class can be accessed from its local inner classes.

Sample
public class Test {
    public static void main(String[] args) {
        final int x = 1;
        class Local {
            void func() {
                System.out.println(x);
            }
        }
        Local l = new Local();
        l.func();
    }
}

Result
1