Java Static Class: The Complete Guide

Java static class is always implemented in the nested inner class. Suppose there are two classes, outer and inner, in which inner is the nested class. Then static can be given to the inner class only.

The benefit of the static class is we didn’t create an object for a static class in the main method.

Java Static Class

Static classes in Java are the way of grouping classes together. Java doesn’t allow you to create the top-level static classes; only nested static inner classes. Only nested classes can be static.

The static keyword can be used with the class, variable, method, and block. Static members belong to a class instead of a particular instance, meaning if you make the member static, you can access it without the object.

Differences between static and non-static class

  1. In a nested static class, we don’t need a reference of the outer class, but a non-static nested class always requires the reference of the outer class.
  2. The static nested class can only access the static method and members of the outer class while a non-static nested class can access both static and non-static members of the outer class.
  3. We create the object of the non-static class in the main method and use that object as the reference of the nested (inner) class. We never create the object of the static class in the main method.

See the following program.

class outer {
  static class inner {
    void display() {
      System.out.println("this is static class");
    }
  }

  public static void main(String args[]) {
    outer.inner obj = new outer.inner(); // we only created the object of outer class
    obj.display();
  }
}

See the following output.

Java Static Class Example

Okay, now let’s see an example of a non-static class in Java. See the following code.

class nextOuter {
  class nextinner {
    void show() {
      System.out.println("this is non-static class");
    }
  }

  public static void main(String args[]) {
    nextOuter outer = new nextOuter();
    nextOuter.nextinner inner = outer.new nextinner();
    inner.show();
  }
}

See the following output.

Differences between static and non-static class

Also, see another example.

class StrangerThings {

    public static class El {
        public El() {
            System.out.println("Eleven has telekinetic power");
        }
    }

    public StrangerThings() {
        System.out.println("StrangerThings Season 3 is awesome");
    }
}

public class SC {

    public static void main(String[] args) {
        StrangerThings str = new StrangerThings();
        StrangerThings.El el = new StrangerThings.El();
    }
}

I’ve added the constructors to both classes to see when they are instantiated. In other words, when those objects are created from them.

We can use the StrangerThings class in the usual way; note that creating a StrangerThings object does not create an El object. 

If we want to instantiate our static class, creating an object from our static El class, we have to use the new separately in the class.

See the output.

➜  java javac SC.java
➜  java java SC
StrangerThings Season 3 is awesome
Eleven has telekinetic power
➜  java

As you can see, the static inner El class acts like an entirely separate class, which happens to be accessed via the outer StrangerThings class. We don’t need the instance of the outer class to create an object of a static inner class. 

The only exception to static classes acting like completely separate classes to their enclosing classes is that static inner classes can access static data members of the enclosing class — or call static methods, for that matter.

So, A class can be made static only if it is nested.

  1. The nested static class doesn’t need the reference of the Outer class.
  2. A static class cannot access non-static members of the Outer class.

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.