I have a class called node. I want to store each nodes parent within each node like this:
public class Node
{
public Node parent;
}
So say I assign parent to a node:
Node n = new Node();
Node n2 = new Node();
n.parent = n2;
If I change n2, will the parent variable of n change too?
No, the
parentvariable ofnwill not change: once a reference is copied, it gets a life of its own. If you change theNodepointed byn2, however,n‘s parent will see that change. For example, if you setn2.parent = n3,n.parent.parentwill change ton3as well.