Static Nested Class in Java

When a class is defined with the static modifier inside the body of another class, it is called a static nested class in Java.

A static nested class is a nested class that is a static member of its enclosing class. Unlike a non-static nested class (inner class), it does not have an implicit reference to an instance of the enclosing class.

Note: A static nested class is not an inner class. According to Java terminology, the term “inner class” specifically refers to a non-static nested class.

Syntax to Declare Static Nested Class in Java


The general syntax to declare a static nested class inside a top-level class is:

// A top-level class.
 public class Outer 
 {
    // Static member class.
    public static class Nested
    {
      // Body for class Nested.
    }
 } 

In this syntax:

  • Outer is a top-level class.
  • Nested is a static nested class declared inside the top-level class Outer.
  • Nested is a static member of class Outer, similar to a static field or a static method.
  • Since Nested is static, you can create its object without creating an object of Outer. You can instantiate it using new Outer.Nested() without creating an instance of Outer.
  • A static nested class can directly access only the static members of Outer. To access the non-static members of Outer, it must use an explicit instance of Outer.
  • An object of Outer and an object of Outer.Nested can exist independently because a static nested class does not have an implicit enclosing instance of Outer.

Relationship between Static Nested Class, Inner Class and Outer Class Object


A static nested class in Java can be declared public, protected, package-private (default access), or private to control its accessibility from outside the enclosing class. The relationship between a static nested class, an inner class, and the enclosing (outer) class object is illustrated in the figure below.

Association of static nested class and inner class with the outer class object in Java.

1. Can an instance variable x exist without an existing outer class object?

An instance variable belongs to an object. Therefore, an instance variable of the outer class cannot exist unless an instance of the outer class exists. That’s why, without an existing outer class object, an instance variable x can never exist.

2. Can a static variable y exist without an existing outer class object?

A static variable belongs directly to the class itself, rather than to any specific object created from that class. Therefore, without an existing outer class object, a static variable y can exist.

3. Can an inner class exist without an existing outer class object?

No, because an inner class is always strongly associated with an enclosing instance of the outer class. Consequently, an instance of the inner class cannot be created without an instance of the outer class. Therefore, without an existing outer class object, an inner class cannot exist.

4. Can a static nested class exist without an existing outer class object?

Yes, because a static nested class is not associated with any enclosing instance of the outer class. Therefore, an instance of the static nested class can be created without creating an object of the outer class. For this reason, it is called a static nested class, not a static inner class. Without an existing outer class object, a static nested class can exist.

5. Can a class itself be static in Java?

The class itself cannot be really ‘static’. Java does not support static top-level classes. A class can be static if it is a nested class.

How to Create an Object of a Static Nested Class in Java?


Since a static nested class is a static member of its enclosing class, we do not need to create an object of the enclosing class to create its object.

A static nested class object does not have an implicit reference to an instance of the enclosing (outer) class. Therefore, it does not share the special instance relationship that a non-static inner class has with its enclosing object.

An object of a static nested class is created in the same way as you create an object of the outer class (top-level class) by using the new keyword.

To create an object of class Nested, we can write directly like this:

Nested n = new Nested(); // n is an object reference variable of class Nested.

We can also create an object of a static nested class from outside the outer class like this:

Outer.Nested  n = new Outer.Nested();

Example 1: Creating an Object of Static Nested Class

Let’s take an example program in which we will declare a static nested class inside an outer class and create its object directly. Look at the following example code.  

public class A 
{ 
// Static nested class starts here. 
   static class B 
   { 
       public void m1() 
       { 
          System.out.println("Static nested class method"); 
       } 
   } // Ends here. 
   public static void main(String[] args) 
   { 
    // Class B is a static nested class, not a regular inner class. 
    // Therefore, the outer class object is not required. 
    // We can directly create an object of static nested class. 
       B b = new B(); 
       b.m1(); 
   } 
}

Output:

Static nested class method

Can We Declare a main() Method within a Static Nested Class?


Yes, we can declare a main method inside a static nested class. A static nested class can have both static and non-static members, including a public static void main(String[] args) method.

Example 2: Declare a main() Method Inside a Static Nested Class

public class OuterClass 
{ 
   static class NestedClass 
   { 
      // Declaration of the main method inside a static nested class. 
      public static void main(String[] args) 
      { 
         System.out.println("Static nested main method"); 
      } 
   } 
   public static void main(String[] args) 
   { 
      System.out.println("Outer class main method"); 
   } 
}

If you run the program using OuterClass, the output will be “Outer class main method”.

If you run the program using OuterClass$NestedClass, the output will be “Static nested main method”.

The source file is compiled only once using javac OuterClass.java; afterward, you can execute either the outer class or the static nested class because both contain a valid main() method.

The Java compiler generates two class files:

  • OuterClass.class
  • OuterClass$NestedClass.class

Access Static and Non-Static Members of Outer Class from Static Nested Class


Accessing static and non-static members from static and non-static nested class

From the above figure, there are the following important facts about a static nested class in Java.

  1. Inside the static nested class, we can declare both static and non-static members, including the main method.
  2. Both static and non-static members of a static nested class can directly access all the static data members (variables) and static methods of its outer class.
  3. A static nested class cannot access non-static members of the enclosing class because it does not have an implicit reference to an enclosing object. To access them, it must use an explicit object of the enclosing class.
  4. A non-static inner class can access accessible static members of a static nested class declared in the same enclosing class by using the nested class name.

Example 3: Accessing Static and Non-Static Members from Static Nested Class

public class Test 
{ 
    int x = 10; 
    static int y = 20; 

    // Static nested class starts here. 
    static class Nested 
    { 
        static int z = 50; 
        static void m1() 
        { 
           // This statement generates compile time error. 
           // Because we cannot make a static reference to the non-static field x. 
           // System.out.println("Value of x: " +x); 
           System.out.println("Value of y: " +y); 
           System.out.println("Value of z: " +z); 
        } 
        // Instance method inside static nested class. 
        void m2() 
        { 
          // This statement generates compile time error.
          // Because we cannot make a static reference to the non-static field x. 
          // System.out.println("Value of x: " +x); 
          System.out.println("Value of y: " +y); 
        } 
     } // Ends here. 
     // Normal inner class starts here. 
     class Inner 
     { 
        void m3() 
        { 
           System.out.println("Value of x: " +x); 
           System.out.println("Value of y: " +y); 
           System.out.println("Value of z: " +Nested.z); 
           Nested.m1(); 

           Nested n = new Nested(); 
           n.m2(); 
        } 
     } 
     public static void main(String[] args) 
     { 
        Test t = new Test(); 
        Test.Inner i = t.new Inner(); 
        i.m3(); 
     } 
}

Output:

Value of x: 10 
Value of y: 20 
Value of z: 50 
Value of y: 20 
Value of z: 50 
Value of y: 20

Can We Create an Object of a Static Nested Class Outside the Outer Class?


Yes, we can create an object of the static nested class from outside the outer class in Java.

Example 4:

Let’s consider an example program where we will create an object of static nested class outside the outer class.

class Outer1 
{ 
    int a = 20; 
    static int b = 30; 
    static class Inner1 
    { 
        void show() 
        { 
          // System.out.println(a); // Compile time error. 
          System.out.println(b); 
        } 
    } 
} 
public class Tester { 
    public static void main(String[] args) 
    { 
       System.out.println(Outer1.b); 
       Outer1.Inner1 i = new Outer1.Inner1(); 
       i.show(); 
    } 
}

Output:

30 
30

FAQs in Interview Questions


Questions:

  1. Can you declare a class inside a class?
  2. Can you declare a class inside an interface?
  3. Can you declare an interface inside a class?
  4. Can you declare an interface inside an interface?

Answer: If the interviewed person asks the above kind of questions, you give a simple answer like this. Anything inside anything is possible with respect to class and interface. All four combinations are valid.

Use of Static Nested Class in Java


There are the following advantages of using a static nested class in Java. They are as follows:

  1. A static nested class can directly access the static members of its outer class, including private static members.
  2. It acts as a direct static member of its top-level class, not a member of the package in which it is declared.
  3. Without an existing outer class object, there may be a chance of an existing static nested class. To access static members of the static nested class, there is no need for an outer class object.

Difference between Inner Class and Static Nested Class


The differences between a normal inner class and a static nested class in Java are the following:

1. In the case of a normal or regular inner class, without an existing outer class object, there is no chance of an existing inner class object. While, in the case of a static nested class, without an existing outer class object, there may be a chance of an existing nested class object.

2. An inner class object is strongly associated with an outer class object, whereas a static nested class is not strongly associated with an outer class object.

3. In a normal inner class, we cannot declare any static members, but in a static nested class, we can declare a static member, including the main method.

4. Since we cannot declare the main method in the normal inner class, we therefore cannot run the inner class directly from the command prompt. But we can declare the main method and can also run the static nested class directly from the command prompt.

5. A normal inner class can access both static and non-static members of the outer class directly, but from the static nested class, we can access only static members.


Key Points of Static Nested Class in Java

  • A class declared inside another class is known as a nested class in Java.
  • There are two main categories of nested classes in Java:
    • Static nested class
    • Inner class (non-static nested class)
  • A member class declared with the static modifier is known as a static nested class.
  • The non-static member class is known as the inner class (or non-static nested class).
  • A static nested class is not an inner class in Java because it is declared with the static modifier and does not require an instance of the enclosing class.
  • It can be declared with any access modifier: public, protected, package-private (default), or private.
  • Since a static nested class is associated with the outer class rather than its objects, it cannot directly access non-static members of the enclosing class. It can access them only through an instance of the enclosing class.
  • An instance of a static nested class is created the same way as you create an instance of a top-level class using the new keyword.
DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.