unknown_boundaries
unknown_boundaries

Reputation: 1580

Creating instances of static nested class within that

This is related to Declaring an instance of a class inside that class

Why the static inner class Node allows the new Node[R] field within the class, and why the infinite recursion is not happening here?

public class TrieST<Value> {
   private static final int R = 256;        // extended ASCII


   private Node root;      // root of trie
   private int N;          // number of keys in trie

  // R-way trie node
  private static class Node {
    private Object val;
    private Node[] next = new Node[R];
  }
...
}

x = new Node(); // is fine within the containing class, but new Node[R] inside static nested Node class is not clear.

Upvotes: 0

Views: 65

Answers (1)

Seelenvirtuose
Seelenvirtuose

Reputation: 20618

The line

private Node[] next = new Node[R];

declares and initializes the field next which is an array reference (you should read Node[] as "Node array"). The expression new Node[R] does not create any node, it creates an array of nodes. So there is no recursion.

If you had a field such as

private Node someNode = new Node();

then indeed you would have a recursion because each node would create another node that creates a next one, and so on.

Upvotes: 3

Related Questions