Java.Core.What is a “static class”?

What is a “Static Class” in Java?

In Java, a class cannot be declared as static at the top level. However, a nested class (a class inside another class) can be static. This is called a static nested class.

Top-Level Classes Are Already Static in Nature

By design, top-level classes are already independent and do not need a static modifier. They are:

  • Loaded once per classloader (just like static members).
  • Accessible anywhere in their package or globally (if public).
  • Not tied to any instance, making static redundant.

Since a top-level class is already independent, marking it static is unnecessary.


1. Static Class (Static Nested Class)

A static nested class is a static class inside another class.

  • It does not have access to instance (non-static) members of the outer class.
  • It behaves like a regular class but is scoped inside the outer class.

Example: Static Nested Class

class Outer {
    static class StaticNested { // Static class inside Outer
        void show() {
            System.out.println("Inside Static Nested Class");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer.StaticNested obj = new Outer.StaticNested(); // No need for Outer instance
        obj.show();
    }
}

Output:

Inside Static Nested Class

Key Points:

  • StaticNested class is a static nested class inside Outer.
  • It does not need an instance of Outer to be created.
  • It cannot access non-static fields/methods of Outer.

2. Why Use a Static Nested Class?

Encapsulation & Organization

  • If a class is only relevant to its outer class, nesting it improves code structure.
  • Example: A DatabaseConnection class inside DatabaseManager.

Memory Efficiency

  • Since static nested classes do not store a reference to the outer class, they consume less memory.

No Dependency on Outer Class Instances

  • A non-static inner class requires an instance of the outer class, while a static nested class does not.

3. Static Nested Class vs. Inner Class

FeatureStatic Nested ClassInner Class
Requires an instance of the outer class?❌ No✅ Yes
Can access outer class instance variables?❌ No✅ Yes
Memory Usage✅ Less (no reference to outer instance)❌ More (stores reference to outer class)
InstantiationOuter.StaticNested obj = new Outer.StaticNested();Outer.Inner obj = outer.new Inner();

4. Common Use Cases for Static Nested Classes

✔ Example 1: Utility Classes (Encapsulation)

class Database {
    static class Connection {
        void connect() {
            System.out.println("Connected to Database");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Database.Connection db = new Database.Connection();
        db.connect();
    }
}

✅ The Connection class only makes sense inside Database, so it is nested.


✔ Example 2: Builder Pattern

class Car {
    private String brand;
    private int year;

    static class Builder {
        private String brand;
        private int year;

        Builder setBrand(String brand) {
            this.brand = brand;
            return this;
        }

        Builder setYear(int year) {
            this.year = year;
            return this;
        }

        Car build() {
            Car car = new Car();
            car.brand = this.brand;
            car.year = this.year;
            return car;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car.Builder().setBrand("Tesla").setYear(2023).build();
        System.out.println("Car: " + car.brand + ", Year: " + car.year);
    }
}

Output:

Car: Tesla, Year: 2023

5. Can We Declare a Top-Level Static Class?

No, a top-level class cannot be static in Java.

static class Example { // ❌ Compilation error
    void display() {
        System.out.println("Static class");
    }
}

Error:Illegal modifier for the class Example; only public, abstract & final are permitted


Final Answer

🔹 A “static class” in Java is actually a static nested class (a class inside another class marked as static).
🔹 It does not require an instance of the outer class and cannot access non-static members of the outer class.
🔹 Used for encapsulation, memory efficiency, and better organization.

This entry was posted in Без рубрики. Bookmark the permalink.