0

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.

5
  • Where do you see a infinite recursion and why should it happen? Commented Jul 23, 2015 at 6:15
  • 3
    new Node[] just initializes the array. It doesn't create R Node objects. Commented Jul 23, 2015 at 6:16
  • While creating a instance of nested class via new Node[R]. I the reason is it's being static then why the construction is allowed via new? Commented Jul 23, 2015 at 6:17
  • 1
    @Prakhar you don´t create a new instance of a node, you just create a Array of the size 256, with 256 elements beeing null. There is no way where a recursion could happen. Commented Jul 23, 2015 at 6:18
  • 1
    Side note: "extended ASCII" is not a precise definition of an encoding. If you're trying to represent a specific encoding, you should state what it is. Commented Jul 23, 2015 at 6:21

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.