Java passes everything by value, so if you use the assignment operator in a class method you're not going to be changing the original object.
For example:
public class Main {
public static void main(String[] args) {
Integer i = new Integer(2);
setToThree(i);
System.out.println(i);
}
public static void setToThree(Integer i) {
i = new Integer(3);
}
}
is going to print 2.
Having said that, if the object you're passing in a reference to is mutable you can make changes to it in they way you're thinking of.
For example:
public class Main {
public static void main(String[] args) {
MyMutableInt i = new MyMutableInt(2);
setToThree(i);
System.out.println(i);
}
public static void setToThree(MyMutableInt i) {
i.set(3);
}
}
This will print 3 (assuming MyMutableInt has a correct toString() method).
Of course, Java Integers are immutable, and so don't have the ability to be changed like that. So you have 2 choices here:
Note: this doesn't work with primitives of any kind. For that you're going to have to pass back by return value. If you have multiple values to mutate, you'll have to wrap them in an object to return them, so you may also use this method.