wellnwill Marathahalli Bangalore

Static nested class


Inner class which is declared as static is known as static nested class.

It is not strongly connected to outer class. We can directly create the object of static nested class and can call members of static nested class.

We can declare static member inside static nested class as it is not possible in normal inner class.


Anonymous inner class which extends one class

class A 
{
  static class B
  {
   static void m1()
   {
    System.out.println("m1() of static nested class");
   }
  }
}
class Test
{
 public static void main(String args[])
 {
  A.B b1=new A.B();
  b1.m1();
 }
}



class A 
{
  static class B
  {
   static void m1()
   {
     System.out.println("m1() of static nested class");
   }
  }

 public static void main(String args[])
 {
  B b1=new B();
  b1.m1();
 }
}




class A 
{
  int a=100;
  static class B
  {
   static void m1()
   {
    System.out.println(a);
   }
  }

 public static void main(String args[])
 {
  B b1=new B();
  b1.m1();
 }
}
O/P-CTE- non static inside static is not allowed.



class A 
{
  int a=100;
  static class B
  {
   static void m1(A a1)
   {
    System.out.println(a1.a);
   }
  }

 public static void main(String args[])
 {
  A a1=new A();
  B b1=new B();
  b1.m1(a1);
 }
}
O/P-100

interface I
{
 void m1(); 
}
interface J
{
 void m2();
}
class C
{}
class A {
static class B extends C implements I,J
  {
   public void m1()
   {
    System.out.println("m1() of static nested class");
   }
   public void m2()
   {
    System.out.println("m2() of static nested class");
   }
  }

 public static void main(String args[])
 {
  B b1=new B();
  b1.m1();
  b1.m2();
 }
}

Footer with Map