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
      {
        . . .
      }
  }
}

Code Snippet

// Local Inner Class in Java

class Outercls {
    void display() {
        class Inner {
            void innerDisplay() {
                System.out.println("Inner Display");
            }
        }

        Inner i = new Inner();
        i.innerDisplay();
    }
}

public class App {
    public static void main(String[] args) {
        Outercls o = new Outercls();
        o.display();
    }
}