user3189985
user3189985

Reputation: 189

Java - pass a variable by reference

I wish to send an 'int' to a function that changes its value. I want to send it by reference so the new value of 'int' is kept after returning to the callee. What is the most effective way to do it?

I know that I can use a wrapper class Integer that sends an object of an 'int'. However, I can't figure out how to change the value of this class, so the change will prevail after returning to the callee.

Thanks for your help.

Upvotes: 0

Views: 96

Answers (1)

NimChimpsky
NimChimpsky

Reputation: 47310

Nothing can be passed by reference in java, you need a wrapper, a conventional bean would do:

public class MyWrapper {
int x;
public void setX(int x){
    this.x = x;
  }
}

Upvotes: 3

Related Questions