wellnwill Marathahalli Bangalore

Method local inner class

A class which is declared inside the method is known as method local inner class.


class Outer 
{
   void show() 
        {
	  class Local
	   {
		 public void show1()
		 {
		 System.out.println("Local class show1()");
		 }
		}
            System.out.println("inside show()");
	Local l=new Local();
	l.show1();
        }
   
}
class Test7
{
 public static void main(String args[])
 {
  Outer o=new Outer();
  o.show();
 }
}



class Outer 
{
  static void show() 
        {
	  class Local
	   {
		 public void show1()
		 {
		 System.out.println("Local class show1()");
		 }
		}
            System.out.println("inside show()");
	Local l=new Local();
	l.show1();
        }
   
}
class Test7
{
 public static void main(String args[])
 {
  Outer o=new Outer();
  o.show();
 }
}


class Outer 
{
   int i;
   static int j;
        void show()
        {
                 class Local
		 {
		 
		 public void show1()
		 {
		 System.out.println("Local class show1()");
		 System.out.println(i);
		 System.out.println(j);
		 }
		 
		}
            
            System.out.println("inside show()");
			Local l=new Local();
			l.show1();
        } 
}
class Test7
{
 public static void main(String args[])
 {
  Outer o=new Outer();
  o.show();
 }
}



class Outer 
{
   int i;
   static int j;
      static  void show()
      {
               class Local
		 {
		 public void show1()
		 {
		 System.out.println("Local class show1()");
		 System.out.println(i);
		 System.out.println(j);
		 }
		}
		
            System.out.println("inside show()");
			Local l=new Local();
			l.show1();
        }
 }
class Test7
{
 public static void main(String args[])
 {
  Outer o=new Outer();
  o.show();
 }
}
//O/P-CTE—non-static i cannot be referenced from static context.


All types of variable will be accessed in method local inner class. The applicable modifier with method local inner class is final, abstract and strictfp.


class Outer 
{
  int a=100; static int b=200;private int c=3000;
        
        void show() 
        {
	    int d=300;final int e=400;
	    
		class Local
		 {
		 public void show1()
		 {
		  System.out.println(a);
		  System.out.println(b);
		  System.out.println(c);
		  System.out.println(d);
		  System.out.println(e);
		 }
		}
		
            System.out.println("inside show()");
			Local l=new Local();
			l.show1();
        }
   
}
class Test8{
 public static void main(String args[]) {
  Outer o=new Outer();
  o.show();
 }
}

Nesting of inner class


We can define n number of classes one inside one as per requirement.


class Test1
{
 class Test2
 {
  class Test3
  {
    void show()
    {
    System.out.println("nesting of classes")
    }
  }
 }
}

class Test
{
 public static void main(String args[])
 {
  Test1 t1=new Test1();
  Test1.Test2 t2=t1.new Test2();
  Test1.Test2.Test3 t3=t2.new Test3();
  t3.show();
 }
}

Footer with Map