by Dinesh Thakur

A local inner class or simply a local class is an inner class that is declared inside a method or constructor or initialization block of the enclosing class. Like regular inner classes, the local classes are associated with a containing instance and can access any member, including the private members of the enclosing class. In addition, it can also access local variables in the method and parameters passed to the method only if the variables are declared final. Just like local variables, the inner class cannot be accessed outside the code block. It is useful when a computation in a method requires the use of specialized class that is not used elsewhere.

The following program shows the use of local variable

class Outer

{
    private double i = 11.5 ;
    private static String str = "Hi Dinesh Thakur";
    public void display()
    {
       class inner
       {
             public void outerInfo()
          {
              System.out.println("i = " + i);
              System.out.println("str = " + str);
           }
       }
              inner innObj = new inner();
              innObj.outerInfo();
    }
}
       class LocalClass
     {
         public static void main(String[] args)
         {
           Outer outobj = new Outer(); //create instance of outer class
           outobj.display();
         }
     }

Local Inner Classes in Java Example