java - Static nested class inside parameterized class -
i looking @ java code implementing binary search tree shown here , thought better if inner class node in fact turned static nested class. however, adding static keyword in front (i thought since inner class not in fact anywhere use instance of enclosing class -- 1 can access in node inner class bst.this -- bound it) resulted in multiple errors not extremely helpful.
as far i'm aware, java.util.linkedlist , similar used static nested classes define nodes stored inside , parameterized (and, of course, work without problems). care elaborate?
thanks.
if make inner class static, you'll lose type parameters of surrounding class. node has no access key , value, since it's no longer associated instance of bst. can fix adding type parameters node too:
private class node<nodekey extends comparable<nodekey>, nodevalue> { private nodekey key; // sorted key private nodevalue val; // associated data private node<nodekey, nodevalue> left, right; // left , right subtrees private int n; // number of nodes in subtree public node(nodekey key, nodevalue val, int n) { this.key = key; this.val = val; this.n = n; } } and replacing every occurence of node node<key, value>
Comments
Post a Comment