Static inner class in java

Ashok Veer | May 21, 2020 | Be the first to comment!
Static inner class in java

In java a static class which is defined inside another outer class is called static nested class or static inner class. A static inner class is nested class which can be accessed without creating the object of the outer class. We can access like static other static members of class.

A static inner class have does not access to the outer class instance variable and methods. Below is syntax of the static nested class.

public class OuterClass
{
     // static nested class
     static class InnerClass
     {
          
     }
}


Below is example of static inner class, here we are just simple creating the inner class object by using the outer class name there is no need to creating the outer class object like which is require in the non-static inner class.



public class OuterClass
{
      // static nested class
      static class InnerClass
      {
            int a = 20;
            int b = 20;
            public int sum(){
                  return a+b;
            }
           
            public static void display(){
                  System.out.println("Static method display in inner class");
            }
      }
     
      public static void main(String[] args) {
            OuterClass.InnerClass innerCls = new OuterClass.InnerClass();
            System.out.println("Sum of a & b is : "+innerCls.sum());
            InnerClass.display();
      }
     
}

Output

Sum of a & b is : 40
Static method display in inner class

In the above program we have created static class inner class named InnerClass inside the outer class named OuterClass.
Check below line here we are creating the object of the inner class using the name of outer class.

OuterClass.InnerClass innerCls = new OuterClass.InnerClass();

Note: It is important to keep in mind that we can access only outer class static member variable directly inside the static inner class.

So for non-static variables access of outer class in inner class we need to create the object of the outer class.

public class OuterClass
{
      String country = "India";
      static String language = "java";
             
      static class InnerClass
      {
            public static void display(){
                  //"Accessing the outer class non-static variable"
                  OuterClass o = new OuterClass();
                  System.out.println(o.country);
                 
                  //"Accessing the outer class static variable"
                  System.out.println(language);
            }
      }
}

Important Note: Inner classes are members of the outer class, so you can apply any access modifiers like private, protected to your inner class which is not possible in normal outer classes.


I hope you enjoyed by reading this article if any suggestion please write down below in comment section.


0 comments:

Post a Comment

 
Copyright © 2019 techfloaters • All Rights Reserved.
Template Design by Ashok Veer ( veersoft solution)