Static Nested Class

It is a common practice to nest the Node class inside the LinkedList class:

public class LinkedList<T> {
  private Node<T> head;
  
  private static class Node<E> {
    E data;
    Node<E> next;
  }
  
  // we can have constructors, methods to add/remove nodes, etc.
}

Note the nested class is declared as static.

A static nested class does not have access to the instance members of the outer class.

On the other hand, the outer class has access to the instance members of the static nested class objects. The Node does not need access to the members of LinkedList, but LinkedList can access data and next on objects of Node, eliminating the need for getters/setters.

Inner vs Static Nested Class
Inner ClassStatic Nested Class
It is an instance member of the outer class (not static!) and has access to all the members of the outer class (private, static, etc.)It is a static member of the outer class and does not have access to the instance members of the outer class. Instead, the outer class has access to the nested class members.

Here is a toy example that showcases the use of inner vs. static nested classes.

Resources