Sobes.tech
Junior

What is a static class?

sobes.tech AI

Answer from AI

In Java, there are no top-level static classes. static can be applied to nested classes.

Static Nested Class:

  • Declared with the static modifier inside another class (outer class).
  • Does not have direct access to the outer class's instance fields and methods, only through an instance of the outer class if needed.
  • Can access the outer class's static fields and methods directly.
  • Is created without creating an instance of the outer class.
  • Used for grouping related classes or for convenient access to utility classes.

Example of creation:

class Outer {
    static class StaticNested {
        // fields and methods of the static nested class
    }
}

Example of usage:

// Creating an instance of the static nested class
Outer.StaticNested nestedObject = new Outer.StaticNested();

Thus, "static class" in the context of Java usually refers to a static nested class.