Java Nested Static Class

Introduction:

Java can also have nested classes i.e. classes inside another class. The following example is a simple demonstration of a nested class.

class A {
  class B {
    //code
  }
  //code
}

Generally, java’s nested classes are of two types:

  • Non-static nested class.
  • Static nested class.

In this article, a small overview of non-nested static classes is given and later we’ll discuss the static nested class in detail.

Non-Static Nested Class

Let’s see a simple example of a nested non-static class.

class outerClass {
  class innerClass {
    void show() {
      System.out.println("Inner class message");
    }
  }
}

Here the outerClass is known as the outer-class and the innerClass is known as the inner-class.

To access the members of the inner class, we need to instantiate that class i.e. create an object of that class. But in nested classes, it works a bit differently. 

In the above example, the innerClass is within the outerClass hence we need to create an object of the outerClass and then use it to create the object of the innerClass.

Let’s see how we can do it.

Code:

Java Code

class Main {
  public static void main(String[] args) {
    outerClass oc = new outerClass();
    outerClass.innerClass ic = oc.new innerClass();
    ic.show();
  }
}
class outerClass {
  class innerClass {
    void show() {
      System.out.println("Inner class message");
    }
  }
}

Output: Inner class message

Now let’s begin with the static nested class.

Static Nested class

Like any other class, static nested classes can include both non-static and static members.

The following example shows the simple structure of a static nested class.

class A {
  static class B {
    // static and non-static members of B
  }
  // members of A
}

Instantiating the inner static class

We saw in the non-static nested class – to access the inner class, first, we need to create an object of the outer class and then use it to create the object of the inner class.

But in static nested classes, we don’t need the object of the outer class. We can directly use the outer class name to create the object of the inner static class. Let’s see how.

Code”

Java Code

class Main {
  public static void main(String[] args) {
    outerClass.innerClass ic = new outerClass.innerClass();
    ic.show();
  }
}
class outerClass {
  static class innerClass {
    void show() {
      System.out.println("Inner class message");
    }
  }
}

Output: Inner class message

Please note that there is a slight change in the syntax of how we are creating the object of the inner class in non-static vs static nested classes.

Accessing members of static inner class

There are two methods to access the members of the static inner class:

  1. By creating the object of the inner class and using it to access the members
  2. By directly accessing the members.

The second method comes in handy when there exists a static member and the user tries to access it. Please note that the first method can be used to access both the static and non-static members of the inner class.

We discussed the first method in the above example, now let’s discuss the second method.

Code:

Java Code

class Main {
    public static void main(String[] args) {
        outerClass.innerClass.show();
    }
}
class outerClass{
    static class innerClass{
        static void show(){
            System.out.println("Inner class message");
        }
    }
}

Output: Inner class message

Accessing members of the outer class

We can access the static members of the outer class within the inner static class by directly using the “class name” and dot operator. Let’s see how.

Code:

Java Code

class Main {
    public static void main(String[] args) {
        outerClass.innerClass ic = new outerClass.innerClass();
        ic.show();
    }
}
class outerClass{
    static void display(){
        System.out.println("Outer class message");
    }
    static class innerClass{
        static void show(){
            outerClass.display(); // outerClass method
            System.out.println("Inner class message");
        }
    }
}

Output:

Outer class message
Inner class message

Here we called the outerClass method display() within the innerClass method show().

If we try to access the non-static members within the inner static class, it gives an error. This happens because static nested classes can only access the static members of the outer class.

Declaring an outer class as static

Let’s see what happens if we declare the outer class as static.

Code:

Java Code

class Main {
    public static void main(String[] args) {
        outerClass.innerClass ic = new outerClass.innerClass();
        ic.show();
    }
}
static class outerClass{
    static class innerClass{
        void show(){
            System.out.println("Inner class message");
        }
    }
}

Output:

From here we can say that only inner classes can be defined as static.

Extending static inner class

Like any other class, we can extend a static inner class with another inner class. Let’s see how.

Code:

Java Code

class Main {
  public static void main(String[] args) {
    outerClass oc = new outerClass();
    outerClass.extendClass ec = oc.new extendClass();
    ec.display(); // sub inner class method
    ec.show(); // super inner class method
  }
}
class outerClass {
  static class innerClass {
    void show() {
      System.out.println("Inner class message");
    }
  }
  class extendClass extends innerClass {
    void display() {
      System.out.println("Extended class message");
    }
  }
}

Output:

Extended class message
Inner class message

Special thanks to Yash Mishra for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this articleIf you want to suggest any improvement/correction in this article please mail us at [email protected]