Consider the following code:
public class Test {
static private volatile Integer number1 = 42;
static private volatile Integer number2 = 42;
public static void main(String[] args) {
Test test = new Test();
test.changeInteger(number1);
System.out.println(number1);
}
public void changeInteger (Integer number) {
number = new Integer(3);
}
}
I would like to change the value of either number1
or number2
depending on the argument I pass to the method changeInteger
. Obviously this doesn't work. Is there a way to make this work in Java without using reflection?
To clarify: I want the call changeInteger(number1)
to change the field number1
.