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
  }
}
Share:
20,594

Related videos on Youtube

Shashi
Author by

Shashi

Software Developer at IBM Contact me on: IBM: [email protected] Alternative: [email protected]

Updated on July 23, 2022

Comments

  • Shashi
    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 of result was ABC when the method was invoked, I need to modify it as DEF and return to caller. Can somebody suggest how do I achieve it?

    • 4castle
      4castle
      Are you referring to java.lang.ref.Reference? If so, this is not what that class is for.
  • John Bollinger
    John Bollinger almost 8 years
    In Java, all arguments are passed by value. This includes references. In runMethod, result is a copy of the Reference reference that was provided as an argument. The two are independent references to the same object.
  • Shashi
    Shashi almost 8 years
    It would have been straight forward if Java passed parameters by Reference. But it's not.
  • Richard Guy
    Richard Guy almost 8 years
    OK, 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
    La VloZ Merrill almost 8 years
    More 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
    John Bollinger almost 8 years
    This 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
    SamTebbs33 almost 8 years
    Java 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
    SamTebbs33 almost 8 years
    @JohnBollinger Ah, I didn't realise that there was a Referencetype in the standard library and assumed it was a third-party type.
  • Richard Guy
    Richard Guy almost 8 years
    This is basically a semantic argument and of no help to the question.
  • Shashi
    Shashi almost 8 years
    Yeah I want to change the contents
  • La VloZ Merrill
    La VloZ Merrill almost 8 years
    He can't change the return type to a type which is not incompatible with the implemented one.
  • Richard Guy
    Richard Guy almost 8 years
    I 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
    Shashi almost 8 years
    Well, 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
    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
    La VloZ Merrill almost 8 years
    @SamTebbs33 unfortunately he coudn't change the return type from int to Reference because Reference type isn't incompatible with 'int`, see this : stackoverflow.com/questions/14694852/…
  • SamTebbs33
    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
    La VloZ Merrill almost 8 years
    @SamTebbs33 we can change return type for overriden methods to only compatible type and Reference isn't compatible with int
  • La VloZ Merrill
    La VloZ Merrill almost 8 years
    @SamTebbs33 the OP said that he must implement an interface which contains that method ;)
  • SamTebbs33
    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
    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
    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.....

Related