0

I'm just beginning to learn java and I'm figuring out method calls. I wrote the code below, so the method 'calling' uses the method 'called' to add 1 to 'number', but it doesn't seem to be executing properly. here's the code:

private void calling(){

    int number1 = 4; 

    int number2 = 2; 

    called(number1);

    System.out.println("number1 = " + number1);
    System.out.println("number2 = " + number2);
}

private void called(int number1){ 

    number1++;

}

and then the output I want is 'number1 = 5' & 'number2 = 2', but instead i'm getting this:

    number1 = 4 
    number2 = 2

is there something I'm missing? thanks in advance!

marina
  • 3
  • 2
  • Arguments in Java are passed by value. `called(x)` copies the value from `x` into the `number1` variable inside the `called` function. But altering the variable inside the function doesn't alter the value outside the function. – khelwood Apr 27 '17 at 08:30
  • @khelwood makes sense. thanks! – marina Apr 27 '17 at 08:40

0 Answers0