I'm having a similar problem as in this question: Array of generic nodes Java
However, making the nested Node class static solves the one problem but creates another for me. I've written a Binary Tree, and each time a Node's pointer isn't supposed to point to anything (e.g. a leaf node's left and right pointers, or the root's parent pointer), it actually points to a special "nil" node, which contains no relevant data. Nil is a member variable of the Binary Tree.
When I create a node, the constructor makes all pointers point to nil. However, if I make the Node class static so I can create an array of nodes (which I need to do for a particular method), I get an error for each pointer that says "Cannot make a static reference to the non-static field nil." But if I change nil to be static, I get an error for it which says "Cannot make a static reference to the non-static type T." (My nodes hold parameterized type objects.)
Here's my Node class:
protected static class Node<T>{
Node left, right, parent;
T object;
protected Node(T x) {
object= x;
left= nil;
right= nil;
parent= nil;
}
}
This is the nil designation and the Binary Tree constructor, which creates the nil node and makes it the root:
protected static Node<T> nil;
public BT() {
nil= new Node<T>(null);
root= nil;
}
How do I allow myself to create an array of nodes without running into these static vs non-static issues?