Java: Modifying parameter passed as a Reference
20,594
Solution 1
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
Solution 2
If you really want to pass as reference, here is a tricky way by wrapping it in an array:
class TestRef{
static void func(int[] arr){arr[0] = -arr[0];}
public static void main(String[] args){
int[] arrI = new int[1];
arrI[0] = 250;
System.out.println(arrI[0]); // 250
func(arrI);
System.out.println(arrI[0]); // -250
}
}
Related videos on Youtube
Author by
Shashi
Software Developer at IBM Contact me on: IBM: [email protected] Alternative: [email protected]
Updated on July 23, 2022Comments
-
Shashi over 1 year
I have to implement an interface method that has the signature like:
public int runMethod(final int key, Reference <String> result);
I have to update the value of
result
parameter before method returns. For example if the value ofresult
wasABC
when the method was invoked, I need to modify it asDEF
and return to caller. Can somebody suggest how do I achieve it?-
4castleAre you referring to
java.lang.ref.Reference
? If so, this is not what that class is for.
-
-
John Bollinger almost 8 yearsIn Java, all arguments are passed by value. This includes references. In runMethod,
result
is a copy of theReference
reference that was provided as an argument. The two are independent references to the same object. -
Shashi almost 8 yearsIt would have been straight forward if Java passed parameters by Reference. But it's not.
-
Richard Guy almost 8 yearsOK, according to the Java spec you are correct. But, can you explain this in a simple way which does not just add more confusion to this simple question? The behaviour is that of pass by reference.
-
La VloZ Merrill almost 8 yearsMore generally, Java always passes by value, either for primitive and refenrene type, more here : docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
-
John Bollinger almost 8 yearsThis correctly explains argument passing, but neither the
Reference
interface nor any of the three implementation classes in the standard library provide a mechanism to change what object an instance refers to. They are not intended for the kind of use the OP seems to be trying to put them to. -
SamTebbs33 almost 8 yearsJava is pass-by-reference (for non-primitive types) since the same object reference is passed to the method, the method parameters just don't have any link to the actual variables that were passed to the method. Only the contents of the object can be changed, and not the variable being passed itself.
-
SamTebbs33 almost 8 years@JohnBollinger Ah, I didn't realise that there was a
Reference
type in the standard library and assumed it was a third-party type. -
Richard Guy almost 8 yearsThis is basically a semantic argument and of no help to the question.
-
Shashi almost 8 yearsYeah I want to change the contents
-
La VloZ Merrill almost 8 yearsHe can't change the return type to a type which is not incompatible with the implemented one.
-
Richard Guy almost 8 yearsI do see the distinction, please see amended answer. Basically, calling result.updateSomething() or result.publicProperty = "something" will work as you want but result = null; will not cause the callers variable to become null as the parameter received by runMethod is not a true pointer.
-
Shashi almost 8 yearsWell, thank you all. I also thought the 'Reference' was from java.lang.Reference. But on further investigation found that it's from a third party and has an assign method.
-
SamTebbs33 almost 8 years@LaVloZMerrill Please check my edit, I simply forgot to change the method's return type in the example code. It was quite late at night.
-
La VloZ Merrill almost 8 years@SamTebbs33 unfortunately he coudn't change the return type from
int
toReference
becauseReference
type isn't incompatible with 'int`, see this : stackoverflow.com/questions/14694852/… -
SamTebbs33 almost 8 years@LaVloZMerrill Well obviously it isn't. If the return type can be changed then it is a way of achieving what the OP wanted.
-
La VloZ Merrill almost 8 years@SamTebbs33 we can change return type for overriden methods to only compatible type and
Reference
isn't compatible withint
-
La VloZ Merrill almost 8 years@SamTebbs33 the OP said that he must implement an interface which contains that method ;)
-
SamTebbs33 almost 8 years@LaVloZMerrill Yes but if you change the return type of the interface's method, then there won't be a problem!
-
La VloZ Merrill almost 8 years@SamTebbs33 of course isn't a problem, but usually you don't change the interface, cause if there's many classes which implement it, it would be a pain to do that, another thing, what if the interface is in a third party library?!!!
-
SamTebbs33 almost 8 years@LaVloZMerrill That's why I first said if you can change the return type, then it is an alternative, if not then don't do it, as I have already said.....