public class Test {
private Result result;
public Test(Result res){
this.result = res;
}
public void alter(){
this.result = Result.FAIL;
}
}
public enum Result{ PASS, FAIL, MORE};
public Result myResult = Result.PASS;
Test test = new Test(myResult);
test.alter();
In the above example, how would I modify the variable myResult inside the alter method? Since Java is pass by value, the example simply assigns its value to this.result.
Basically, you can’t, because Java is pass-by-value.
The closest you can get to pass-by-reference behavior in Java is to create a “holder” class with a getter and setter; e.g.
Then, you could write
alter()as:Note that this is not real pass-by-reference.