Local Inner Class in Java


A class is a non-primitive or user-defined data type in Java, while an object is an instance of a class. Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.

This section covers the following topics: Declaring Local Classes. Accessing Members of an Enclosing Class. A class i.e. created inside a method is called local inner class in java. If you want to invoke the methods of local inner class, you must instantiate this class inside the method.

Syntax:
   class Class_Name   // OuterClass
   {
      void method ( )
      {
          class Class_Name  // NestedClass
          {
           . . .
          }
      }
   }

Source Code

//Local Inner Class
 
class Outercls
{
    void display()
    {
        class Inner
        {
            void innerDisplay()
            {
                System.out.println("Inner Display");
            }
        }
 
        Inner i =new Inner();
        i.innerDisplay();
    }
}
 
public class localInnerClass {
    public static void main(String[] args) {
        Outercls o =new Outercls();
        o.display();
    }
}
 

Output

Inner Display
To download raw file Click Here
`

Basic Programs