by Dinesh Thakur Category: Inheritance

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

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article