What does a static inner class mean? How is it different from any other static member

Showing Answers 1 - 19 of 19 Answers

Arundathi

  • Jul 1st, 2005
 

A static inner class behaves like any ``outer'' class. It may contain methods and fields. 
 
It is not necessarily the case that an instance of the outer class exists even when we have created an instance of the inner class. Similarly, instantiating the outer class does not create any instances of the inner class .  
 
The methods of a static inner class may access all the members (fields or methods) of the inner class but they can access only static members (fields or methods) of the outer class. Thus, f can access the field x, but it cannot access the field y.  

Sanket

  • Apr 17th, 2007
 

Class Test
{
   int i ;

   Test ()

   {

     i = 10;

   }

   public static void main(String arg[])
   {
       final Test t = new Test();
                 Test m = new Test();
   
         class inner()
       {
             inner()
           {
            System.out.println(t.i); //Allow               Inner Class is default is static can                                                                           acess static member
            System.out.println(m.i); // Not Allow
            }
       }

   }  

}

In Above Example it shows that inner class access member of Outer calss by final obje only...cause final default behave as static .. inner class can access only static variable static method of outer class...
Even u can define method's n var in inner class...

but only static method can use only static data.

  Was this answer useful?  Yes

sandhya

  • Apr 19th, 2007
 

A class with static keyword considered as staic inner class and a static class behaves like a nested class of outer class. We can create object of static inner class in other class also

class outer
{
         int m=9;
         void m()
      {
                     System.out.println("hai");
         }
               static class inner
            {
               int x=10;
               void m2()
                     {
                           System.out.println("hello");
                     }
               }
}
class maindemo
{
   public static void main(String[] args)
{
outer o=new outer();
o.m();
outer.inner ouin=new outer.inner();
ouin.m2();

}

}

the underlined block is not possible with general inner classes

  Was this answer useful?  Yes

padmavthi

  • Jun 6th, 2007
 

A Static Inner Calss is a nested class.Because it is static it must access the members of its enclosing class through an Object.It cannot refer to members of its enclosing class directly.

Moreover A non-Static nested  class  is called  inner class.

It has access to all of the variables and methods of  its enclosing  or outer class,and may refer to them  directly in the same way that other non static members of the  outer class do.

  Was this answer useful?  Yes

A static inner class is a static member of the enclosing class

class BigOuter{
    static class Nested{}
}
A static inner class has no access to instance variables and non static members of the enclosing class

  Was this answer useful?  Yes

A static member class or a static member interface comprises the same declarations as those allowed in an ordinary top-level class or interface.

A static member class must be declared as a static member of an enclosing class or interface.

 Nested interfaces are considered implicitly static, the keyword static can, therefore, be omitted. Since static member classes and interfaces are members of an enclosing class or interface, they can have any member accessibility.


Static member classes and interfaces can only be nested within other static member or top-level classes and interfaces.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions