returning a value from a method to another method

前端 未结 8 586
时光取名叫无心
时光取名叫无心 2021-01-19 14:42

Can somebody tell me why the value returned is 3 and not 8. Doesn\'t the return x statement from the addFive method change the value of x

8条回答
  •  旧巷少年郎
    2021-01-19 15:03

    You are calling the method addFive(int x) with x, but not assigning the returned value to anything. So, inside main()'s scope x remains as before, 3 - which is what is being printed. So, you can either store the returned value to x itself:

    x = addFive(x);
    

    or make the function call within print statement:

    System.out.println("x = " + addFive(x));
    

提交回复
热议问题