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 a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.