Java > Object-Oriented Programming (OOP) > Nested and Inner Classes > Static Inner Classes
Static Inner Class Example: Accessing Outer Class Members
This example demonstrates how a static inner class interacts with its outer class. Specifically, it highlights the ability of a static inner class to access only static members of the outer class.
Code Example
The OuterClass
contains a static inner class called StaticInnerClass
. The inner class has a method, displayOuterStaticVariable()
, that accesses the static variable outerStaticVariable
of the outer class. Crucially, the inner class cannot directly access the instance variable outerInstanceVariable
. Trying to do so would result in a compilation error. The main
method creates an instance of the static inner class and calls its method to demonstrate the accessibility of static members.
public class OuterClass {
private static int outerStaticVariable = 10;
private int outerInstanceVariable = 20;
public static class StaticInnerClass {
public void displayOuterStaticVariable() {
System.out.println("Outer Static Variable: " + outerStaticVariable);
// Cannot access outerInstanceVariable directly from here
// System.out.println("Outer Instance Variable: " + outerInstanceVariable); // Compilation error
}
}
public static void main(String[] args) {
OuterClass.StaticInnerClass inner = new OuterClass.StaticInnerClass();
inner.displayOuterStaticVariable();
}
}
Concepts Behind the Snippet
Static inner classes are associated with the outer class itself, not with instances of the outer class. Therefore, they can only access the static members of the outer class. This is because they don't implicitly hold a reference to an instance of the outer class (unlike non-static inner classes). They are like static methods in this respect. The static
keyword in the inner class declaration is what gives it these properties.
Real-Life Use Case
Static inner classes are often used as helper classes within an outer class. A good example is when you have a class representing a data structure, and you want to create a helper class to perform some operation on that data structure, without needing access to the specific instance of the data structure. Think of a class that provides static utility methods acting on instances of the outer class but is itself independent.
Best Practices
Use static inner classes when you want to logically group a class with another class, and the inner class doesn't need access to the outer class's instance members. This helps to encapsulate related code and improve organization. Avoid unnecessary use of static inner classes; consider whether a regular top-level class would be a better choice.
Interview Tip
Be prepared to explain the key difference between static and non-static (inner) nested classes. A static inner class cannot access the non-static members of the outer class, while a non-static inner class can. Also, be ready to discuss the use cases for each.
When to Use Them
Use static inner classes when you want to create a utility class that is tightly coupled to the outer class but doesn't require access to the outer class's instance state. For example, it could be used for implementing a builder pattern or a data structure-specific helper.
Memory Footprint
Static inner classes do not hold an implicit reference to an instance of the outer class. Therefore, using a static inner class can potentially reduce the memory footprint compared to a non-static inner class, especially if many instances of the inner class are created and the outer class instance would not otherwise be needed.
Alternatives
If the inner class needs to access instance members of the outer class, you should use a regular (non-static) inner class. If the inner class is not logically associated with the outer class, you should consider using a separate top-level class.
Pros
Cons
FAQ
-
What is the main difference between a static inner class and a regular (non-static) inner class?
A static inner class does not have access to the instance members of the outer class and is not associated with a specific instance of the outer class. A regular inner class has access to all members of the outer class (including private members) and is associated with a specific instance of the outer class. -
How do you create an instance of a static inner class?
You create an instance of a static inner class using the following syntax:OuterClass.StaticInnerClass inner = new OuterClass.StaticInnerClass();