Java Language Nested and Inner Classes Method Local Inner Classes


A class written within a method called method local inner class. In that case the scope of the inner class is restricted within the method.

A method-local inner class can be instantiated only within the method where the inner class is defined.

The example of using method local inner class:

public class OuterClass {
    private void outerMethod() {
       final int outerInt = 1;
        // Method Local Inner Class
        class MethodLocalInnerClass {
            private void print() {
                System.out.println("Method local inner class " + outerInt);
        // Accessing the inner class
        MethodLocalInnerClass inner = new MethodLocalInnerClass();

    public static void main(String args[]) {
        OuterClass outer = new OuterClass();

Executing will give an output: Method local inner class 1.