Java Interlude

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 objects of the static nested class. This is the desired arrangement: the Node does not need access to the members of LinkedList, but LinkedList can access data and next on objects of Node thus eliminate the need for getters/setters.

Inner vs Static Nested Class
Inner ClassStatic Nested Class
Instance member of the outer class (not static!) and as such have access to all the members of the outer class (private, instance, static, etc.)Static (class) member of the outer class and do not have access to instance members. Rather, the outer class has access to the nested class members for convenience.

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