Hello I was wondering how I would be able to send a variable as a parameter to a method and have it be changed by the method. For example
public class Main {
public static void main(String[] args) {
int i = 2;
doThis(i);
System.out.println(i);
}
public static void doThis(int i) {
i = 3;
}
}
I would like it to print out 3 instead of 2. Thanks.
I would like it to print out 3 instead of 2
Change method to return value
int i = 2;
i = doThis(i);
public static int doThis(int i) {
i = 3;
return i;
}
it copies the value of primitive from caller to argument
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With