A static nested class is a class declared inside another class using the static keyword. Unlike a non-static inner class, it does not require an object of the outer class to be created.
What Is a Static Nested Class?
Think of an outer class as a container and the static nested class as a closely related helper type. The nested class belongs to the outer class's namespace, but it does not belong to any particular outer-class object.
class Computer { static class Processor { void start() { System.out.println("Processor started"); } } }
Here, Processor is a static nested class because it is declared with static inside Computer.
Creating a Static Nested Class Object
Because a static nested class does not depend on an outer-class instance, you can create its object directly using the outer class name.
class Computer { static class Processor { void start() { System.out.println("Processor started"); } } } public class Main { public static void main(String[] args) { Computer.Processor processor = new Computer.Processor(); processor.start(); } }
Notice the difference from a non-static inner class. There is no Computer object before creating the Processor object.
Important: The syntax is OuterClass.NestedClass. You do not write outerObject.new NestedClass() for a static nested class.
Why Use a Static Nested Class?
A static nested class is useful when a class is strongly related to its outer class but does not need access to a particular outer object's instance state.
For example, suppose an application has a Product class and a Builder used to construct products. The builder is related to the product, but it does not need an existing product object.
class Product { private String name; private double price; static class Builder { private String name; private double price; Builder setName(String name) { this.name = name; return this; } Builder setPrice(double price) { this.price = price; return this; } Product build() { Product product = new Product(); product.name = name; product.price = price; return product; } } }
The Builder is logically associated with Product, but it does not require a pre-existing Product instance. This is a common real-world use of static nested classes.
Accessing Outer Class Members
A static nested class can access the outer class's static members directly. However, it cannot directly access non-static instance members because no outer object is automatically associated with it.
class Company { static String companyName = "Tech Solutions"; String location = "Bhubaneswar"; static class Information { void display() { System.out.println(companyName); // System.out.println(location); } } }
The nested class can access companyName because it is static. It cannot directly access location because that field belongs to a particular Company object.
Accessing an Instance Member Through an Object
A static nested class can still work with an outer instance if an object is explicitly provided.
class Company { private String location = "Bhubaneswar"; static class Information { void display(Company company) { System.out.println(company.location); } } } public class Main { public static void main(String[] args) { Company company = new Company(); Company.Information information = new Company.Information(); information.display(company); } }
The nested class does not magically receive an outer object. Instead, the required object is explicitly passed to the method.
Static Nested Class with Private Outer Members
A static nested class can access private members of the enclosing class when it has an appropriate object reference for instance members.
class Account { private String owner = "Amit"; static class Viewer { void show(Account account) { System.out.println(account.owner); } } }
The private access restriction does not prevent the nested class from accessing the member. The important point is that the instance must be explicitly available.
Static Nested Class vs Inner Class
| Feature | Static Nested Class | Inner Class |
|---|---|---|
| static keyword | Required | Not used |
| Outer object required | No | Yes |
| Direct instance-member access | No | Yes |
| Creation syntax | new Outer.Nested() | outer.new Inner() |
| Typical purpose | Related type without outer-instance dependency | Type tied to an outer object |
Static Nested Class and Static Members
A static nested class can contain both static and instance members. It behaves much like a regular class, with the additional benefit of being logically grouped inside the outer class.
class Database { static class Config { static final String HOST = "localhost"; int port = 5432; void display() { System.out.println(HOST); System.out.println(port); } } } public class Main { public static void main(String[] args) { System.out.println(Database.Config.HOST); Database.Config config = new Database.Config(); config.display(); } }
The static field can be accessed directly through the nested class name, while the instance field requires a Config object.
Real-World Example: Builder Pattern
One of the most recognizable uses of a static nested class is the Builder pattern. A builder can be placed inside the class it constructs because the two concepts are strongly related.
class Employee { private final String name; private final int age; private Employee(Builder builder) { this.name = builder.name; this.age = builder.age; } static class Builder { private String name; private int age; Builder name(String name) { this.name = name; return this; } Builder age(int age) { this.age = age; return this; } Employee build() { return new Employee(this); } } }
The builder does not need an existing Employee object. That is exactly why a static nested class is a natural fit here.
Common Beginner Mistakes
- Creating an outer-class object unnecessarily before creating a static nested class.
- Assuming a static nested class can directly access outer instance fields.
- Confusing a static nested class with a static method.
- Using a static nested class when the nested object genuinely needs an outer instance.
- Using deep nesting simply to reduce the number of files.
Best Practices
- Prefer a static nested class when no outer instance state is required.
- Use it to keep strongly related helper types together.
- Use clear access modifiers to control whether the nested type is part of the outer class's public API.
- Consider a top-level class when the nested type becomes widely reusable or conceptually independent.
Quick Revision
| Concept | Key Point |
|---|---|
| Static nested class | Nested class declared using static |
| Outer object | Not required to create the nested object |
| Outer instance fields | Cannot be accessed directly without an outer object |
| Static outer members | Can be accessed directly |
| Common use | Helper classes and Builder implementations |
Interview Insight
The most important interview distinction is this: a static nested class does not carry an implicit reference to an enclosing outer-class instance. A non-static inner class does. This difference affects object creation, memory relationships, and which outer members can be accessed directly.
Remember: If the nested class does not need a particular outer object, static is often the cleaner choice. Think of it as a related class living inside the outer class's namespace, not as a part of one specific outer object.
Static nested classes give Java a useful middle ground: you can keep closely related types together without creating an unnecessary dependency on an outer object. Once this distinction becomes clear, choosing between inner and static nested classes becomes much easier.
