Scientech Easy Static Nested Class in Java | Uses & Example Programs | Scientech Easy

Saturday, March 9, 2019

Static Nested Class in Java | Uses & Example Programs

Static Nested Class in Java


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.
              }
         } 
 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.
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. 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 an object of a 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:
    Outer.Nested  n=new Outer.Nested();

Let's see a simple example program.
Program source 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 a main method be declared within a 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 a practical simple example program to understand the concept.
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 an Outer class from Static nested class


Static nested class in java
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 see a practical example program to understand the concept.
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 an object of static nested class outside the outer class?


Yes, we can create an object of the static nested class from 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); } } } package staticNestedClass; 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
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?
Key points:
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.

Uses of a static nested class in Java


There are the following advantages of using a static nested class in Java.
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


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.

Final words 
Hope that this article has covered almost all important points related to the topic static nested class in Java with example programs. We hope that you have enjoyed this article.
Thanks for reading!