What is the difference between inner class and nested class?


When a class is defined within a scope of another class, then it becomes inner class. 


class A
{
   class B //inner class
   {
   }
}



If the access modifier of the inner class is static, then it becomes nested class.


class A
{
   static class B //nested class
   {
   }
}

Use a non-static nested class (or inner class) if you require access to an enclosing instance's non-public fields and methods. 


Use a static nested class if you don't require this access




Comments