returning a value from a method to another method

前端 未结 8 584
时光取名叫无心
时光取名叫无心 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:08

    Like everyone else is saying, you need to assign your return value. Because you're doing "addFive(x)" instead of "x=addFive(x);" you're just printing the instance of "x" in main, and not ever getting the value that your function returns.

    This is because "x" in your main function is an instance variable, and your "x" in addFive() is a local variable. These are not the same variable, even if they have the same name. This might clarify a bit - http://www.tutorialspoint.com/java/java_variable_types.htm

提交回复
热议问题