Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 954
abhirathore2006 Avatar asked Aug 26 '26 01:08

abhirathore2006


1 Answers

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.

like image 92
Eran Avatar answered Aug 27 '26 15:08

Eran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!