I'm new at learning Java and I wanted to try my luck with linked lists. It all worked perfectly, but now I want to replace all direct calls to attributes of the nodes with get and set methods. There is however one method that troubles me. I wanted to implement a setNode method that would set the current node as another node.
Here is some of the relevant code:
public class ListNode {
int value;
ListNode next;
// more code exists in-between but it shouldn't be related (theoretically speaking :P)
public void setNode(ListNode node) {
this = node;
}
}
When I try to compile the program, I get the following error message:
ListNode.java:32: error: cannot assign a value to final variable this
this = node;
^
So I was wondering if there is some sort of a workaround, and also why we can do this without issues:
node = node.next;
// or
node = node2;
for example, but not this:
this = this.next;
// or
this = node2;