Local inner class or method local inner class

If you are not familiar with nested classes, then you should read them first. We have discussed the member inner class in the previous topic. The member inner class is the simple version of an inner class. In this post, we will see another inner class that is local inner class in java or method local inner class which is considered a complicated version of inner class.

Here is the table content of the article will we will cover this topic.
1. What is method local inner class?
2. Important points local inner class in Java
3. How to instantiate method local inner class in java?
4. Access modifier with method local inner class
5. Access members in method local Inner class
6. Can we use static keyword in method local inner class?

What is method local inner class?

We can define a class inside a block or method that is called a local inner class or method local in class. A method local inner class can’t be declared as a member of the class. The scope of method local inner class is limited, just as a local variable. We can’t access it from outside the block in which it is defined. Generally, programmers declare the class in a method that’s why it is called method local inner class. You can declare a class inside of for loop, or an if clause also.

local inner class in java
public class OuterClass 
{
	void outerClassMethod()
	{
		// Creating method inner class 
		class InnerClass
		{
			public void innerClassMethod()
			{
				System.out.println("Method of inner class");
			}
		}
	}
	
        public static void main(String[] args) 
        {
        	//Body of main method
        }
}

Important points local inner class in Java

1. A local inner class or method local inner class can’t be declared as a member of the outer class. The method local inner class belongs to the block/method in which they are defined.
2. The method local inner class can access the fields of the Outer class.
3. We can’t use any access modifiers with method local inner class. It means the method local inner class can’t use private, public, or protected access modifiers with it.
4. We can use use the abstract keyword and final keyword with method local inner class.
5. The method local inner class should be instantiated within a block/method where it is created.  You can’t instantiate it from outside the block/method.
6. A method local inner class in Java can access only the final local variable of the enclosing block. But from JDK 8, it is possible to access the non-final local variable of enclosing block in the local inner class.
7. Method local inner class can extend an abstract class or can also implement an interface.

How to instantiate method local inner class in java?

To create an object of method local inner class we need to choose the right place. We can create an object only after the place where its definition ends in the method. Unlike member inner class, We don’t need the object of the outer class to create an object of method local inner class.

public class Outerclass
{
   void outerClassMethod() 
   {
      // method-local inner class
      class InnerClass 
      {
         public void innerClassMethod() 
         {
            System.out.println("Method of inner class");       
         }   
      } // end of inner class
        
      // Accessing the inner class
      InnerClass obj = new InnerClass();
      obj.innerClassMethod();
   }
    
   public static void main(String args[]) {
      Outerclass outer = new Outerclass();
      outer.outerClassMethod();           
   }
}

Output: Method of inner class

Access modifier with method local inner class

A method local inner class can’t declare with the private, protected, and public access modifiers. It can be only a default access modifier and we already know if we are not defining any access modifier then it is considered as default. A method local inner class is not associated with any object. If we will try to use any modifier with the class name the compiler will show the compilation error. We can only use the abstract keyword or final keyword with it.

public class Outerclass
{
   void outerClassMethod() 
   {
      // method-local inner class
      public class InnerClass 
      {
         public void innerClassMethod() 
         {
            System.out.println("Method of inner class");       
         }   
      } // end of inner class
        
      // Accessing the inner class
      InnerClass obj = new InnerClass();
      obj.innerClassMethod();
   }
    
   public static void main(String args[]) {
      Outerclass outer = new Outerclass();
      outer.outerClassMethod();           
   }
}

Output: Compilation error

Access members in method local Inner class

As we know a local inner class can be defined in the method. Like a member inner class, the method local inner class can access all the variables of the outer class. Even they are private members or not.

public class Outerclass
{
   int a = 5;
   final int b = 10;
   void outerClassMethod() 
   {
	   int c = 15;
	   final int d = 20;
      // method-local inner class
      class InnerClass 
      {
         public void innerClassMethod() 
         {
            System.out.println("Value of a :"+a);       
            System.out.println("Value of b :"+b);
            System.out.println("Value of c :"+c);
            System.out.println("Value of d :"+d);
         }   
      } // end of inner class
        
      // Accessing the inner class
      InnerClass obj = new InnerClass();
      obj.innerClassMethod();
   }
    
   public static void main(String args[]) {
      Outerclass outer = new Outerclass();
      outer.outerClassMethod();           
   }
}

Output: Value of a :5
Value of b :10
Value of c :15
Value of d :20

Can we use static keyword in method local inner class?

1. We can’t use the static keyword with the method local inner class. If we try to make it as static the compiler will throw an exception.

2. A method local inner class can’t have any static variable or static method. It can have only non-static variables. If we use the static keyword in the method local inner class, only with the final keyword. It means we can use the static final variable only.

public class Outerclass
{
   void outerClassMethod() 
   {
      // method-local inner class
      class InnerClass 
      {
    	 int a = 5;
    	 static int b =10; // Show compilation error
   	 static final int c = 20; // We can use it
         public void innerClassMethod() 
         {
            System.out.println("Value of a :"+a);       
            System.out.println("Value of b :"+b);
            System.out.println("Value of c :"+c);
         }   
      } // end of inner class
        
      // Accessing the inner class
      InnerClass obj = new InnerClass();
      obj.innerClassMethod();
   }
    
   public static void main(String args[]) {
      Outerclass outer = new Outerclass();
      outer.outerClassMethod();           
   }
}

2 thoughts on “Local inner class or method local inner class”

  1. // + inner class in method, must be defined before use it.
    void display() {
    Record recordObj = new Record(); // error
    class Record {}
    }

    Reply

Leave a Comment