Java Method-Local Inner Class


To create a class within the method which is called method-local inner class. A local inner class can be instantiated only within the method where the inner class is defined.


Syntax:

class OuterClass {
	Return_type MethodName(){
        class InnerClass {
        	//code
        }
    }
}  

Example

public class Oclass {
   // instance method of the outer class 
   void showdata() {
      int Age = 45;
      // local inner class
      class MethodInnerclass {
         public void showresult() {
            System.out.println("Age is "+Age);	   
         }   
      } 
      // Accessing the inner class
      MethodInnerclass ic = new MethodInnerclass();
      ic.showresult();
   }
   public static void main(String args[]) {
      Oclass oc = new Oclass();
      oc.showdata();	   	   
   }
} 



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.