Static Nested Class in Java with Example

When an inner class is defined with a static modifier inside the body of another class, it is known as a static nested class in Java.

A static nested class is also considered a top-level class, nested top-level class, or static member class in javaBut it is not considered an inner class.

The following snippet of code declares a top-level class Outer and a static nested class Nested.

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

Since it is a static nested class, we do not need an instance of its outer class to create its object. An object of class Outer and an object of class Nested can exist independently because both are top-level classes.

Relationship between Static Nested class, Inner class with Outer class object


Java static nested class can be declared public, protected, package-level, or private to restrict its accessibility outside its outer class.

An association of static nested class and inner class with the outer class object is shown in the below figure.

Static nested class in Java

Key points:   

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

The answer is no because an instance variable is always part of an object. That’s why without existing an outer class object, an instance variable x can never exist.

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

The answer is yes. This is because the static variable is not anywhere related to a particular object. It is always part of the class level. Therefore, without existing an outer class object, a static variable y can exist.


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

The answer is no because an inner class is always strongly associated with the outer class object. Therefore, without existing an outer class object, an inner class cannot exist.


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

The answer is yes because a static nested class is not strongly associated with the outer class object. Therefore, without existing an outer class object, a static nested class can exist.

Therefore, the word comes nested instead of inner for the static class and it is called the static nested class in Java, not static inner class.

5. The class itself cannot be really ‘static’. There is no such thing as a static class in Java. A class can be static if it is a nested class.

How to Create Object of Static Nested Class in Java?


Since a static nested class in Java is considered as a top-level class, we do not need to create an outer class object to create its object.

A static nested class object does not share any special relationship with an object of the outer class.

An object of a static nested class is created in the same way as you create an object of the outer class or 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(); // b is 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();

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

Program code 1:   

package staticNestedClass; 
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) 
{ 
// Since class B is a static nested class, not a regular inner class, 
// Therefore, the outer class object is not required. 
// You can directly create an object of static nested class like this. 
     B b = new B(); 
      b.m1(); 
   } 
}
Output: 
       Static nested class method

Can We Declare Main Method within Static Nested Class?


Yes, we can declare the main method inside a static nested class. A static nested class can have both static and non-static members including the main method.


Let’s take an example program in which we will declare main() method inside nested class.

Program code 2:

package staticNestedClass; 
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 compile the above code as OuterClass, the output will be “Outer class main method”.

But if you compile the above code as OuterClass$NestedClass, the output will be “Static nested main method”.

How to 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 java static nested class.

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 the static nested class can directly access all the static data members (variables) and static functions (methods) of its outer class.

3. A static nested class cannot access non-static members of its outer class because it does not have an implicit reference to an outer object.

4. Non-static members of a normal inner class can access the static members of any static nested class within the same outer class.


Let’s take an example program based on the above important points of static nested class.

Program code 3:

package staticNestedClass; 
public class Test 
{ 
  int x = 10; // Instance variable. 
  static int y = 20; // Static variable. 

// Static nested class starts here. 
  static class Nested 
  { 
// static member. 
   static int z = 50; 
   static void m1() 
   { 
   // System.out.println("Value of x: " +x); // Compile time error because we cannot make a static reference to the non-static field x. 
      System.out.println("Value of y: " +y); 
      System.out.println("Value of z: " +z); 
   } 
// Non-static member (Instance method) inside static nested class. 
   void m2() 
   { 
    // System.out.println("Value of x: " +x); // Compile time error because we cannot make a static reference to the non-static field x. 
       System.out.println("Value of y: " +y); 
   } 
  } // Ends here. 
// Normal inner class starts here. 
    class Inner 
    { 
 // Non-static members. 
     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 object of Static Nested Class outside Outer class?


Yes, we can create an object of the static nested class from outside the outer class. Let’s take an example program where we will create an object of static nested class outside the outer class.

Program code 4:

package staticNestedClass; 
public 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:

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 then you tell 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 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 Normal inner class and Static nested class in Java


The difference between normal inner class and static nested class is as follows:

1. In the case of a normal or regular inner class, without an existing outer class object, there is no chance of existing inner class object

whereas, in the case of a static nested class, without an existing outer class object, there may be a chance of 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 normal inner class, we cannot declare any static members but in the 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, therefore, we cannot run 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:

1. A class declared inside another class is known as nested classes in java.

2. There are two types of nested classes available in Java: static and non-static.

3. The member class declared with a static modifier is known as static nested class.

4. The non-static member class is known as inner class or non-static nested class.

5. A static nested class is not an inner class in java because it is considered as top-level class.

6. It can be declared public, protected, package-level, or private to restrict its accessibility.

7. 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.


Hope that this tutorial has covered almost all important points related to static nested class in Java with example programs. I hope that you have understood this topic and enjoyed it.

If you find anything incorrect in this tutorial, then please inform us through email or message. In the next tutorial, we will understand OOPs concepts in Java.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love