Java inner class and static nested class: a complete guide for beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Nested classes in Java: where did it all begin?

Imagine: you are writing code in Java, and suddenly you realize that one class is logically inseparable from another. For example, Car and Engine. An engine does not exist on its own — it is part of a car. How can you model this beautifully and correctly in Java? The answer is nested classes.

The topic of Java inner class and static nested class is one of the most common on Stack Overflow. Thousands of developers get confused: what is the difference? When to use inner and when to use static nested? Why were they even invented?

In this tutorial, we will break everything down. You will not just learn the syntax — you will understand the logic of Java designers. Let's go!



What is a nested class?

Let's start with the basics. A nested class is a class declared inside another class. In Java, there are four types of nested classes, but today we are interested in the two most popular:

  • Static nested class
  • Inner class (also known as non-static nested class)


The key difference is the static modifier. It changes everything: from access to fields to the lifetime of the object. Let's figure it out in practice.



Static nested class: simple and independent

A static nested class behaves like a regular outer class, but "lives" inside its parent. It does not have access to non-static fields and methods of the outer class (unless it receives a reference to the object).



A real-world example: Database and DatabaseConfig. The configuration is not tied to a specific database instance.



public class Database {    private String url;    private static String defaultDriver = "com.mysql.cj.jdbc.Driver";

public Database(String url) { this.url = url; }

// Static nested class public static class DatabaseConfig { private String driver; private int poolSize;

public DatabaseConfig(String driver, int poolSize) { this.driver = driver; this.poolSize = poolSize; }

public void printConfig() { // Has access only to static members of the outer class System.out.println("Default driver: " + defaultDriver); // System.out.println(url); // ERROR! url is a non-static field }

public static void staticMethodInNested() { System.out.println("Static method in a static nested class"); } }

public void useConfig() { // Creating an instance of static nested class DatabaseConfig config = new DatabaseConfig("oracle.jdbc.OracleDriver", 10); config.printConfig(); }}


Notice: to create a DatabaseConfig, we do not need a Database object. This is the main advantage — independence.



When to use a static nested class?

  • When the nested class is logically related to the outer class but does not depend on its instance.
  • When you need to group helper classes (builders, configs, utilities).
  • When you are designing the Builder pattern — a classic example.


Inner class (non-static nested class): forever linked

The inner class is the complete opposite. It is always tied to a specific object of the outer class. This means:

  • It has access to all fields and methods of the outer class (even private ones).
  • It cannot exist without an instance of the outer class.
  • It implicitly holds a reference to the outer object (OuterClass.this).


Example: Car and Engine. Each car

Blogs

Book Recommendations