by Dinesh Thakur

A static nested class or a top-level class is a class that is declared inside another class with a static modifier. Like static methods, a static nested class can only refer directly to static members of the enclosing classes, even if those members are private.

The following example demonstrates the use of static nested class.

class outer

{
      private double i = 11.7 ;
      private static String str = "Hi Programmers";
      static class inner
    {
        int j ;
        public void display()
        {
           j = 51;
          System.out.println("j = " + j);
          //System.out.prinln("i = "  +  i);   illegal statement
          System.out.println("str = " +  str);
        }
     }
}
class StaticClass
   {
       public static void main(String[] args)
      {
          outer.inner innobj =new outer.inner(); //create instance of                                       
                                            //static nested class
        innobj.display();
      }
   }

Static Nested Classes Java Example

As static keyword is used in the innerclass so it is a static class. The str variable is declared static so it can be accessed in the static class. The variable i is not declared static so it cannot be accessed in the class