13

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.

Yuushi
  • 25,132
  • 7
  • 63
  • 81
Tommy
  • 965
  • 6
  • 13
  • 21
  • 2
    Java use pass by value so you can't pass in a primitive and modify the primitive value. You could pass in an array on integers and then modify the integers in the array or return a different integer. – camickr May 12 '13 at 03:39
  • 1
    http://stackoverflow.com/questions/5614562/how-to-pass-by-reference-in-java look at this. – Bill May 12 '13 at 03:40
  • 1
    http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – Steve P. May 12 '13 at 03:40

5 Answers5

11

Java can't do this. However you can return the value from the method...

public static int doThis(int i) {
    return 3;
}

And reassign it...

int i = 2;
i  = doThis(i);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
7

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.

Barney Govan
  • 644
  • 4
  • 5
6

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

jmj
  • 237,923
  • 42
  • 401
  • 438
1

java is all about pass by values. so it wont change the way you wanted. Read here for more

however you do following to solve this problem

i = doThis(i);

public static int doThis(int i) {
    i = 3;
    return i;
}
Community
  • 1
  • 1
Peeyush
  • 422
  • 3
  • 13
1

Java is pass by value for primitive data type or objects.
Reference data type parameters, such as objects, are also passed into methods by value.

gifpif
  • 4,507
  • 4
  • 31
  • 45