Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Understanding Java Nested Static Classes: Usage, Differences, and Examples

Java Nested Static Class

Discover how Java’s nested static classes work, how they differ from inner classes, and when to use them with practical code examples.

In Java, a class can be defined inside another class. These are called nested classes and come in two forms:

While inner classes were covered in our previous tutorial, this article focuses on static nested classes.


Java Nested Static Class

To declare a nested class as static, simply prepend the static keyword:

class Animal {
    static class Mammal {
        // static and non‑static members of Mammal
    }
    // members of Animal
}

Note: Only nested classes can be declared static; top‑level classes cannot.

Like any class, a static nested class can contain both static and instance members.

Because it is a static member of its outer class, you can access a static nested class without creating an instance of the outer class.


Example: Static Nested Class

class Animal {
    // inner class
    class Reptile {
        public void displayInfo() {
            System.out.println("I am a reptile.");
        }
    }

    // static nested class
    static class Mammal {
        public void displayInfo() {
            System.out.println("I am a mammal.");
        }
    }
}

class Main {
    public static void main(String[] args) {
        // create outer class instance
        Animal animal = new Animal();

        // create instance of non‑static inner class
        Animal.Reptile reptile = animal.new Reptile();
        reptile.displayInfo();

        // create instance of static nested class
        Animal.Mammal mammal = new Animal.Mammal();
        mammal.displayInfo();
    }
}

Output

I am a reptile.
I am a mammal.

In the code above, Mammal is a static nested class while Reptile is an inner class.

Notice the different instantiation syntax:


Accessing Members of the Outer Class

Because a static nested class is associated with its outer class, it can access only static members of the outer class. Attempts to reference instance members will result in a compilation error.

Example: Attempting to Call a Non‑static Method

class Animal {
    static class Mammal {
        public void displayInfo() {
            System.out.println("I am a mammal.");
        }
    }

    class Reptile {
        public void displayInfo() {
            System.out.println("I am a reptile.");
        }
    }

    public void eat() {
        System.out.println("I eat food.");
    }
}

class Main {
    public static void main(String[] args) {
        Animal animal = new Animal();
        Animal.Reptile reptile = animal.new Reptile();
        reptile.displayInfo();

        Animal.Mammal mammal = new Animal.Mammal();
        mammal.displayInfo();
        mammal.eat(); // ❌ Compile‑time error
    }
}

Output

Main.java:28: error: cannot find symbol
    mammal.eat();
          ^
  symbol:   method eat()
  location: variable mammal of type Mammal
1 error
compiler exit status 1

The error occurs because mammal is an instance of a static nested class and cannot invoke the non‑static eat() method of Animal.


Static Top‑Level Class?

Java prohibits static top‑level classes. Only nested classes can be declared static. The following code demonstrates the compiler error:

static class Animal {
    public static void displayInfo() {
        System.out.println("I am an animal");
    }
}

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

Output

Main.java:1: error: modifier static not allowed here
static class Animal {
       ^
1 error
compiler exit status 1

This confirms that only nested classes can be static.


Java

  1. C# Static Keyword: Mastering Static Variables, Methods, and Classes
  2. Understanding C# Nested Classes: Definition, Usage, and Inheritance
  3. Mastering Java Nested and Inner Classes: Types, Examples, and Best Practices
  4. Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
  5. Mastering Java’s ObjectInputStream: A Comprehensive Guide
  6. Mastering Java ObjectOutputStream: Serialization, Methods, and Practical Examples
  7. Mastering Java’s PrintStream Class: Print, Println, and Printf Explained
  8. Mastering Java Reader Class: Subclasses, Methods, and Practical Example
  9. Mastering Java Generics – Building Reusable, Type‑Safe Code
  10. Mastering Java File Operations with java.io – Creation, Reading, Writing & Deletion