S07L16 – Few more thing – static inner class in Java

Static Inner Classes in Java

Table of Contents

  • 1. Introduction
  • 2. What are Static Inner Classes?
  • 3. Accessing Static Inner Class Members
  • 4. Working with Private Static Entities
  • 5. Example Code and Explanation
  • 6. Conclusion
  • 7. Keywords

1. Introduction

Java allows you to nest classes within other classes. One powerful type of nested class is a static inner class.
Static inner classes allow grouping classes together, improving modularity and encapsulation within your code.
While not as commonly used in real-world development, understanding static inner classes is beneficial for specific scenarios and a “good to know” concept.

This article explores static inner classes in Java, focusing on their importance, how to implement them, and when to use them.

2. What are Static Inner Classes?

A static inner class is a nested class declared as static inside an outer class. Unlike regular inner classes,
a static inner class does not have access to the instance variables and methods of the outer class. However, it can directly access
the static members of the outer class.

Static inner classes are helpful when you need to group functionality together but don’t require a direct relationship with the outer class’s instance.

Syntax:

3. Accessing Static Inner Class Members

Static inner class members are accessed through the outer class, but since the inner class is static, you don’t need an instance of the outer class
to access the static inner class.

Example:

Once you create an instance of the static inner class, you can call its methods and access its static fields.

4. Working with Private Static Entities

Sometimes, you may need to access private static elements from within a static inner class. Though it’s not considered a best practice to make static elements private, if required, you can access them through specific methods provided by the outer class.

Example:

In the above code, the static inner class StaticInnerClass is able to access the private static variable brand from the outer class.

5. Example Code and Explanation

Let’s take a look at a complete Java program that demonstrates how to use static inner classes and access static members:

Explanation:

  • Private Static Member: The Car class has a private static member brand. The static inner class Model accesses this private static member through the outer class.
  • Creating an Instance of the Static Inner Class: In the main method, an instance of the static inner class Model is created without needing an instance of the Car class.
  • Accessing Static Members: The method displayBrand in the inner class prints the brand, demonstrating how static inner classes can access the outer class’s static fields.

Output:

6. Conclusion

Static inner classes are a useful feature of Java, providing a way to group related classes together.
While static inner classes can access the outer class’s static members, they cannot access non-static fields and methods directly.
Understanding how and when to use static inner classes can enhance the modularity and clarity of your Java programs. Though they are rarely used in practical applications, they are a great tool to understand for specific use cases.