I have the following java class, that has an instance variable property as a generic type passed when declaring the class.
When I try to assign the property passing it as a method argument, it does not seem to change its value if I change the value of the argument itself.
Code:
public class BST<T extends Comparable<T>, S>{
private Node<T, S> bstRoot;
public Node<T,S> getRoot() {
return bstRoot;
}
public void setRoot(Node<T,S> root) {
this.bstRoot = root;
}
public boolean put(T key, S value){
Node<T,S> node = new Node<T,S>(key, value);
return put(node, bstRoot);
}
private boolean put(Node<T,S> node, Node<T,S> root){
if(root == null){
root = node;
}
else{
if(root.compareTo(node) < 0){
put(node, root.getRightChild());
}
else{
put(node, root.getLeftChild());
}
}
return true;
}
}
When I do the following:
public class BSTTest {
public static void main(String[] args){
BST<Integer, String> bst = new BST<Integer, String>();
bst.put(10, "Hello10");
}
}
After doing the put, the bstRoot is still null, instead of being set to a value of Node object with key 10 and value Hello10. Is it not passing by reference then?