Passing in a reference to a method means the reference is a value. This value is made available to you by the name of the method parameter root
, which lives on the stack. Assigning different references to the parameter root
will be reflected only inside the method, but as soon as you leave the method, everything on the stack is lost.
What you like to achieve is a modification of the value hold by the variable my_root
. To do that, you need to pass in a reference to the variable my_root
and not only the value hold by my_root
. This can be done, for example, by passing in a container which holds references to Node
s. A Java Collection
is one of those containers. Passing in the container and modifying its values will be reflected outside the method NodeModify(Node root)
.
Some side notes:
- in Java a method should start with a lower-case letter or an underscore
- use camel-case for variable names instead of underscores to separate words, except when you define constants