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
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));