Search code examples
javatriestatic-class

How recursive inner static class get initialized?


i was looking into Trie data-structure and came across this piece of code

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

i understood the logic, but what i don't get is, to what depth the Node will get initialized ?

you can check complete code at http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/TrieST.java.html


Solution

  • There is no recursion here.

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

    doesn't create any Node instance. It creates a Node[] (array of Node references) of 26 elements. All the references are initialized to null. The Node instances referenced by the array must be initialized elsewhere.

    If, on the other hand, you had a member initialized as:

    private Node n = new Node ();
    

    that would cause an infinite recursion once the first instance of Node would be created.