Sobes.tech
Junior — Middle

Why is the static keyword used when declaring classes in Java?

sobes.tech AI

Answer from AI

In Java, the keyword static when declaring a class is used to create a nested static class (static nested class). Such a class is not associated with an instance of the outer class and can be created without creating an object of the outer class. This is convenient when the nested class is logically related to the outer class but does not require access to its non-static members.

Example:

public class Outer {
    static class Nested {
        void display() {
            System.out.println("This is a static nested class");
        }
    }
}

// Usage:
Outer.Nested nested = new Outer.Nested();
nested.display();