You can't modify the variable that you passed to the method. For example:
public int runMethod(final int key, Reference <String> result) {
result = null; // Only changed the method's version of the variable, and not the variable that was passed to the method
}
...
Reference<String> ref = ...
runMethod(0, ref);
// ref is still what you originally assigned it to
However, you can modify the fields and call the methods of the object you pass.
public int runMethod(final int key, Reference <String> result) {
result.someField = ...; // Here we are changing the object, which is the same object as what was passed to the method.
}
...
Reference<String> ref = ...
runMethod(0, ref);
// ref.someField has now been changed
An alternative would be to change the method's return type to Reference<String>
and return the updated value.
public Reference<String> runMethod(final int key, Reference <String> result) {
return ...;
}
...
Reference<String> ref = ...
ref = runMethod(0, ref);
// ref is now whatever you returned from the method