When an inner class is defined with static modifier inside the body of another class, it is known as a static nested class in Java . It is also considered as a nested top-level class in Java . A static nested class is not an inner class. The following snippet of code declares of a top-level class Outer and a static nested class Nested.

Syntax:

// A top-level class.

public class Outer

{

// Static member class.

public static class Nested

{

// Body for class Nested.

}

}

Difference between Normal inner class and Static nested class in Java

Can we create Object of Static Nested Class outside Outer class?

Accessing of Static and Non-static members of Outer class from Static nested class

How to create Object of Static Nested Class in Java?

A static nested class can be declared public, protected, package-level or private to restrict its accessibility outside its outer class. Now, look at the below image.

Key points:

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.

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.

2. Line 2: Can a static variable y exist without an existing outer class object? The answer is yes. 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.

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.

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 is considered as a top-level class, Therefore, 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 B, you can write directly like this:

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



If you want to create an object of a static nested class from outside the outer class, you write like this:

Let’s see an example program based on concepts of static nested class in java

Can main method be declared 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 see an example program where we will declare main() method inside nested class. Program source 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“.

Accessing of Static and Non-static members of Outer class from Static nested class



From the above figure, there are the following important facts about 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 member (variable) and static function (method) 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 nested class.

Program source 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 source 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 on Interview 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 interview 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

1. In 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 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 command prompt. But we can declare the main method and can also run the static nested class directly from the command prompt.



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

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

Thanks for reading!!!

Next ⇒ Final keyword in Java⇐ PrevNext ⇒