Michael Michael - 1 month ago 14
Java Question

Both instances of custom "Node" class prints the same values when each instance is passed unique values

I am trying to create a new node based on an Entry object. I retrieve 2 different entries and print out their key and value. Both of the keys printed out are different. I store each key and value into seperate "Nodes", n1, and n2. Node is a custom class I created, the code for it can be found below. When I print out the value of n1's key and n2's key, they should be different since the key of the entry was, but for some reason it prints out the same value. I'm not sure why this happens and I can't figure it out. This is the code I have currently:

Node Class:

import java.util.HashMap;
import java.util.Map.Entry;

public class Node {
public static Entry<String, Integer> keyVal;
public static Node leftChild;
public static Node rightChild;
public static String bitCode;

public Node() {
keyVal = null;
leftChild = null;
rightChild = null;
}

public Node(Entry<String, Integer> entry) {
keyVal = entry;
leftChild = null;
rightChild = null;
}

public Node(Entry<String, Integer> entry, Node n1, Node n2) {
keyVal = entry;
leftChild = n1;
rightChild = n2;
}

public Entry<String, Integer> getEntry() { return keyVal; }

public Node getLeftChild() { return leftChild; }

public Node getRightChild() { return rightChild; }

public void setLeftChild(Node lc) {
leftChild = lc;
}

public void setRightChild(Node rc) {
leftChild = rc;
}
}


createTree method in Main class:

private static Node createTree(PriorityQueue<Entry<String, Integer>> pq) {
map = new HashMap<>(); // Defined as a public attribute to the Main class
Entry<String, Integer> entry1 = null;
Entry<String, Integer> entry2 = null;
Entry<String, Integer> parent = null;
Node n1 = null;
Node n2 = null;
Node n3 = null;
System.out.println("PQ size: " + pq.size());

while (pq.size() > 1) {
entry1 = pq.poll();
// Prints "Entry1: R 1"
System.out.println("Entry1: " + entry1.getKey() + " " + entry1.getValue());

entry2 = pq.poll();
// Prints "Entry2: e 1"
System.out.println("Entry2: " + entry2.getKey() + " " + entry2.getValue());

// Never enters if, goes to else
if (map.containsKey(entry1.getKey())) {
System.out.println("Map contains entry1!");
n1 = map.get(entry1.getKey());
} else {
n1 = new Node(entry1);
}
// Never enters if, goes to else
if (map.containsKey(entry2.getKey())) {
System.out.println("Map contains entry2!");
n2 = map.get(entry2.getKey());
} else {
n2 = new Node(entry2);
}
// Should print "N1: R 1", Instead prints "N1: e 1"
System.out.println("N1: " + n1.getEntry().getKey() + " " + n1.getEntry().getValue());
// Prints "N2: R 1"
System.out.println("N2: " + n2.getEntry().getKey() + " " + n2.getEntry().getValue());
}

Answer

This happens because the members in class Node are static:

//From the 'Node' class:
public static Entry<String, Integer> keyVal;
public static Node leftChild;
public static Node rightChild;
public static String bitCode;
//...

So, when you create a second Node, doing:

 n1 = new Node(entry1);

You override the value entered in the first place, at:

n2 = new Node(entry2);