0

i started learning Java some days ago. I have some experience with C++ and this is why I'm used to pointers, which would make this much easier.

so now my question.

Let's say I have the following Code:

public class Main {

    public static void main(String[] args) {

        int variable1=2, variable2=2;

        input(variable1, variable2);

        System.out.println(variable1 + " " + variable2); 
            // this should output "1 3" on the console  
    }

    private static void input(int variable1, int variable2) {

        System.out.println(variable1 + " " + variable2); 
            // this will output "2 2" on the console

        variable1 = 1;
        variable2 = 3;
    }   
}

So the function input() takes the Variables from main and outputs them (correctly). But how can I transfer the changed Variables back to the main function?

Duncan Jones
  • 59,308
  • 24
  • 169
  • 227
globus243
  • 533
  • 1
  • 12
  • 25

7 Answers7

1

In Java, all arguments are passed by value and not by reference. Hence, the code above does not work. If you like to pass a modification back you have to pass an object reference (which is passed by value, but now you pass the object). You have to use a mutable object of course, e.g., a collection. In your case, you might also pass an array of integers.

Of course: all this is bad programming style. You should pass results by return values and use a more functional programming when possible.

Christian Fries
  • 14,119
  • 9
  • 51
  • 65
1

As others have said, primitives are pass-by-value. You cannot pass a reference to them. However, you can pass an instance of any Object and (assuming it is mutable) modify it and the changes will be available to any scope that has a reference to that instance.

For your example, you could do two things -- Use an array...

public class Main{

  public static void main(String[] args){

     int[] values = {2, 2};

     input(values);
     System.out.println(values[0] + ", " + values[1]); // prints 1, 3
  }

  private static void input(int[] values){
      values[0] = 1;
      values[1] = 3;
  }
} 

or with an object

public class ValueHolder{
    private int val1;
    private int val2;

    public void setValue1(int i){ val1 = i; }
    public void setValue2(int i){ val2 = i; }
    public int getValue1(){return val1;}
    public int getValue2(){return val2;}

    public String toString(){ 
          return String.valueOf(val1) + ", " + String.valueOf(val2);
    }
}

public class Main{

  public static void main(String[] args){

     ValueHolder vh = new ValueHolder();
     vh.setValue1(2);
     vh.setValue2(2);
     System.out.println(vh); // prints 2, 2
     input(vh);
     System.out.println(vh); // prints 1, 3
  }

  private static void input(ValueHolder vh){
      vh.setValue1(1);
      vh.setValue2(3);
  }
} 
MadConan
  • 3,589
  • 1
  • 13
  • 25
0
input(Variable1, Variable2);

When this method is invoked, the value of the two variables are copied to the new defined two variables int Variable1, int Variable2. You can think of it like this:

private static void input(int Variable1, int Variable2) {
    Variable1 = 2;
    Variable2 = 2;

    System.out.println(Variable1 + " " + Variable2); // this will output "2 2" on the console

    Variable1 = 1; // this is different from the one that is inside main()
    Variable2 = 3; // this is different from the one that is inside main()
}

Here is an answer of mine on another question that might help you to understand the concept of passing-by-value: https://stackoverflow.com/a/9404727/597657

Community
  • 1
  • 1
Eng.Fouad
  • 107,075
  • 62
  • 298
  • 390
0

Passing primitives in Java is done by value.

You can remove the static method, and use the primitives as instance properties of the Main class, as demonstrated below:

public class Main {
    int firstVariable = 1, secondVariable = 2;
    // instance block of Main, prints the fields
    {
        System.out.println("First variable: " + firstVariable);
        System.out.println("Second variable: " + secondVariable);
    }
    // main (static) method: initializes an instance of Main class and calls
    // (now) instance method "changeInstanceFields", then prints the change
    public static void main(String[] args) {
        Main main = new Main();
        main.changeInstanceFields();
        System.out.println("First variable: " + main.firstVariable);
        System.out.println("Second variable: " + main.secondVariable);
    }
    // instance method: accesses Main class' instance fields
    public void changeInstanceFields() {
        firstVariable = 1;
        secondVariable = 3;
    }
}

Output:

First variable: 1
Second variable: 2
First variable: 1
Second variable: 3
Mena
  • 45,491
  • 11
  • 81
  • 98
0

set it like a class variable

public class Main {

public int variable1, variable2;

public static void main(String[] args) {

    Variable1=2;
    Variable2=2;

    input(Variable1, Variable2);

    System.out.println(Variable1 + " " + Variable2); 
        // this should output "1 3" on the console  
}

private static void input(int Variable1, int Variable2) {

    System.out.println(Variable1 + " " + Variable2); 
        // this will output "2 2" on the console

    Variable1 = 1;
    Variable2 = 3;
}   

}

xedo
  • 989
  • 3
  • 16
  • 31
0

int is primitive type http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html. Integer is boxing int. Both of them do not change their values. instead new objects are created. However, you can wrap int value with a class. See example:

public class Main {
int v;

@Override
public String toString() {
    return "Main [v=" + v + "]";
}

public static void main(String... strings) {
    int v1 = 2;
    int v2 = 3;

    input(v1, v2);
    System.out.println(v1 + " " + v2);

    Integer i1 = 2;
    Integer i2 = 3;

    inputI(i1, i2);
    System.out.println(i1 + " " + i2);

    Main m1 = new Main();
    m1.v = 2;
    Main m2 = new Main();
    m2.v = 3;

    inputM(m1, m2);
    System.out.println(m1 + " " + m2);

}

public static void input(int v1, int v2) {
    v1 = 3;
    v2 = 4;
}

public static void inputI(Integer v1, Integer v2) {
    v1 = 3;
    v2 = 4;
}

public static void inputM(Main v1, Main v2) {
    v1.v = 3;
    v2.v = 4;
}

}

Output:

2 3
2 3
Main [v=3] Main [v=4]
Margarita Litkevych
  • 2,076
  • 18
  • 28
0

This happens because the values of the variables are copied to the method input(). Primitive variables are always copied between methods.