English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Java Other Topics

Java Nested Static Classes

In this tutorial, you will learn about nested static classes with examples. You will also understand the difference between static classes and inner classes.

As described in the previous tutorial, we can have a class within another class in Java. Such a class is called a nested class. In Java, there are two types of nested classes:

  • Nested Non-Static Classes (Inner Classes)

  • Nested Static Classes.

In the previous tutorial, we have discussed inner classes. If you want to learn about inner classes, please visitJava Nested Classes.

In this tutorial, we will learn about nested static classes.

Java Nested Static Class

We use the static keyword to make our nested class static.

Note:In Java, nested classes are allowed to be static only.

Like regular classes, nested classes can contain both static and non-static fields and methods. For example,

Class Animal {
   static class Mammal {
      // Static and non-static members of the Mammal class
   }
   // Member of the Animal class
}

Static nested classes are associated with the outer class.

To access a static nested class, we do not need an object of the outer class.

Example: Static Nested Class

class Animal {
  //Nested Class
   class Reptile {
      public void displayInfo() {
        System.out.println("I am a reptile.");
      }
   }
  //Static Class
   static class Mammal {
      public void displayInfo() {
        System.out.println("I am a mammal.");
      }
   }
}
class Main {
   public static void main(String[] args) {
      //Creating an Object of the Outer Class
      Animal animal = new Animal();
      //Object Creation of Non-static Classes
      Animal.Reptile reptile = animal.new Reptile();
      reptile.displayInfo();
      //Object Creation of Static Nested Classes
      Animal.Mammal mammal = new Animal.Mammal();
      mammal.displayInfo();
   }
}

Output Result

I am a reptile.
I am a mammal.

In the above program, we have two nested classes Mammal and Reptile in the Animal class.

To create an object of the non-static class Reptile, we use

Animal.Reptile reptile = animal.new Reptile()

To create an object of the static class Mammal, we use

Animal.Mammal mammal = new Animal.Mammal()

Accessing Members of the Outer Class

In Java, static nested classes are associated with the outer class. This is why static nested classes can only access the class members (static fields and methods) of the outer class.

Let's see what happens if we try to access the non-static fields and methods of an outer class.

Example: Accessing Non-static Members

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();
 }
}

Output Result

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

In the above example, we created a non-static method eat() in the Animal class.

Now, if we try to access the eat() method using the object mammal, the compiler will display an error.
This is because mammal is an object of a static class, so we cannot access non-static methods from a static class.

Static Top-Level Class

As mentioned above, only nested classes can be static. We cannot have static top-level classes.

Let's see what happens if we try to make a top-level class static.

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 Result

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

In the above example, we try to create a static class Animal. Since Java does not allow the use of static top-level classes, an error will occur.