Inner Class

1) Normal Inner Class : For a normal inner class a instance of the outer class is mandatory and through that instance a inner class instance class instance can be created. The inner class can access even the private variable of the outer class but not vise versa.
Two class files created would be A.class and A$B.class after you compile.

class  A
{
    private int x = 10;
    class B
    {
        int y=20;
        void met1()
        {
            System.out.println(x+" "+y);
        }
    }
}

class Inner1
{
    public static void main(String args[])
    {
        A m = new A();
        A.B n = m.new B();
        n.met1();
    }
}

OUTPUT :    10    20

2) Static Inner Class : Inner classes can not have static keywords but in case it has one the whole class should be declared as static inner class.

class  A
{
    static int x = 10;
    class B
    {
        static int y=20;
        static void met1()
        {
            System.out.println(x+" "+y);
        }
    }
}
class Inner2
{
    public static void main(String args[])
    {
        A.B.met1();
        System.out.println(A.x);
        System.out.println(A.B.y);
    }
}

OUTPUT : Compile Error – inner classes can not have static declaration

class  A
{
    static int x = 10;
    static class B
    {
        static int y=20;
        static void met1()
        {
            System.out.println(x+" "+y);
        }
    }
}
class Inner2
{
    public static void main(String args[])
    {
        A.B.met1();
        System.out.println(A.x);
        System.out.println(A.B.y);
    }
}

OUTPUT :    
10    20
10
20

3) Inner class inside a method : Any outer class method variable which will be accessed in the inner class should be declared as final.
The inner class instance should be created and the inner class methods should be called before exiting the outer class method.

class M
{
    int x = 10;
    void met1()
    {
        final int y=20;
        class N
        {    int z = 30;
            void met2()
            {
                System.out.println(x+" "+y+" "+z);
            }
        }
        N p = new N();
        p.met2();
        System.out.println("Back");
    }
}
class Inner3
{
    public static void main(String args[])
    {
        M a = new M();
        a.met1();
     }
}

OUTPUT :    
10    20    30
Back