Java: Modifying parameter passed as a Reference -


i have implement interface method has signature like:

public int runmethod(final int key, reference <string> result); 

i have update value of result parameter before method returns. example if value of result abc when method invoked, need modify def , return caller. can suggest how achieve it?

you can't modify variable passed method. example:

public int runmethod(final int key, reference <string> result) {     result = null; // changed method's version of variable, , not variable passed method } ... reference<string> ref = ... runmethod(0, ref); // ref still assigned 

however, can modify fields , call methods of object pass.

public int runmethod(final int key, reference <string> result) {     result.somefield = ...; // here changing object, same object passed method. } ... reference<string> ref = ... runmethod(0, ref); // ref.somefield has been changed 

an alternative change method's return type reference<string> , return updated value.

public reference<string> runmethod(final int key, reference <string> result) {     return ...; } ... reference<string> ref = ... ref = runmethod(0, ref); // ref whatever returned method 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -