Static Classes In Java

In Java, a static class is a class that can be accessed without an instance of the class. A static class is defined by adding the static keyword to the class declaration.

Here is an example of a static class in Java:

public class Main {
  public static void main(String[] args) {
    StaticClass.someMethod();
  }
}

static class StaticClass {
  public static void someMethod() {
    System.out.println("Hello from a static class!");
  }
}

In this example, the StaticClass class is a static class, and the someMethod method is a static method. The someMethod method can be called directly on the class, without the need to create an instance of the class.

A static class can only contain static members (fields and methods). It cannot contain non-static members or inner classes.

A static class is often used to group related utility methods or constants, or to define a helper class that is used by another class. It is also sometimes used as a lightweight alternative to a singleton class.

Note that a static class is different from a top-level class with static members. A top-level class is a class that is not nested inside another class, and it can contain both static and non-static members.