Question

I'm trying to implement a tree using a nested Node, which itself has a Node array. (see the code bellow) When I instantiate it and debug it I can see a reference inside the start Node that is called this$0, it basically seems to be the same reference as the instantiated Tree itself. I was wondering if anyone could tell me why it is there and what purpose it serves (if it is not due to some mistake in the code). Thanks.

public class NodeTree {
    private Node start;
    private int degree;

    public NodeTree() {
        start = new Node();
    }

    private class Node {
        private Object root;
        private Node[] subtrees;
        private int size;

        Node() {  }      
    }
}
Was it helpful?

Solution

When you see this$_something_ in a debugger, it means that your class has a reference to the object of an outer class. This reference is created automatically by the compiler.

In your code this happens because Node is a non-static class that is nested inside the NodeTree class. This means that it gets a reference to its outer object, i.e. the NodeTree set automatically.

If you do not want this behavior, make Node static in the NodeTree, or move it out to make it a top-level class:

public class NodeTree {
    private Node start;
    private int degree;

    public NodeTree() {
        start = new Node();
    }
    static private class Node {
//  ^^^^^^
        private Object root;
        private Node[] subtrees;
        private int size;
        Node() {  }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top