VOOZH about

URL: https://www.geeksforgeeks.org/java/static-class-in-java/

⇱ Static class in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Static class in Java

Last Updated : 23 Jul, 2025

Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes.   The class in which the nested class is defined is known as the Outer Class. Unlike top-level classes, Nested classes can be Static. Non-static nested classes are also known as Inner classes.

Note: The top level class cannot be static in java, to create a static class we must create a nested class and then make it static.

An instance of an inner class cannot be created without an instance of the outer class. Therefore, an inner class instance can access all of the members of its outer class, without using a reference to the outer class instance. For this reason, inner classes can help make programs simple and concise. 

Remember: In static class, we can easily create objects.

Differences between Static and Non-static Nested Classes

The following are major differences between static nested classes and inner classes. 

  1. A static nested class may be instantiated without instantiating its outer class.
  2. Inner classes can access both static and non-static members of the outer class. A static class can access only the static members of the outer class.

Example of Static and Non-static Nested Classes

Below is the implementation of topic mentioned above:


Output
Message from nested static class: GeeksForGeeks
Message from non-static nested class: GeeksForGeeks
Message from non-static nested class: GeeksForGeeks
Comment
Article Tags:
Article Tags: