Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why class Node in LinkedList defined as static but not normal class [duplicate]

In package java.util.LinkedList, the Class Node defined as one static class, is it necessary? What is the goal?

We can find source code from this page.

like image 634
Ivan Avatar asked Jan 03 '17 07:01

Ivan


People also ask

What is class Node in linked list?

Each element (we will call it a node) of a list is comprises of the data and a reference to the next node and previous node. The last node has a reference to null. Each element in a linked list is node,which actually holds the data ,previous and next references.

Why are nested classes static?

In Java a static nested class is essentially a normal class that has just been nested inside another class. Being static, a static nested class can only access instance variables of the enclosing class via a reference to an instance of the enclosing class.

What is Node in Linked list in Java?

In Java, a LinkedList is a data structure that stores elements in a non-contiguous location. It is a linear data structure. Each data item is called a 'Node' and each node has a data part and an address part. The address part stores the link to the next node in the LinkedList.

Why do we need a Node class?

The design with the Node class has the further potential advantage that you can design and code a generic linked list and use it to instantiate a list of students, a list of teachers, a list of courses, etc.


1 Answers

Instances of static nested classes have no reference to an instance of the nesting class. It's basically the same as putting them in a separate file but having them as nested class is a good choice if the cohesion with the nesting class is high.

Non-static nested classsed however require an instance of the nesting class to be created and instances are bound to that instance and have access to it's fields.

As example, take this class:

public class Main{

  private String aField = "test";

  public static void main(String... args) {

    StaticExample x1 = new StaticExample();
    System.out.println(x1.getField());


    //does not compile:
    // NonStaticExample x2 = new NonStaticExample();

    Main m1 = new Main();
    NonStaticExample x2 = m1.new NonStaticExample();
    System.out.println(x2.getField());

  }


  private static class StaticExample {

    String getField(){
        //does not compile
        return aField;
    }
  }

  private class NonStaticExample {
    String getField(){
        return aField;
    }
  }

The static class StaticExample can be instantiated directly but has no access to the instance field of the nesting class Main. The non-static class NonStaticExample requires an instance of Main in order to be instantiated and has access to the instance field aField.

Coming back to your LinkedList example, it's basically a design choice.

Instances of Node do not require access to fields of the LinkedList, but putting them in a separate file also makes no sense, as the Node is an implementation detail of the LinkedList implementation and are of no use outside that class. So making it a static nested class was the most sensible design choice.

like image 153
Gerald Mücke Avatar answered Oct 19 '22 04:10

Gerald Mücke