Java methods, when is "return" really necessary?

https://www.codecademy.com/courses/learn-java/lessons/learn-java-methods/exercises/review

In this last task I am supposed to return the “amountToWithdraw” but I can’t figure out why?
I made the “withdraw” method to a void, removed the “return amountToWithdraw” statement and my code just worked finde.
And even if it makes a difference, then why didn’t I have to do it in the previous method “deposit”?

I thought returning something only makes sense if I want to have acceses to it from outside the method, which doesn’t seem to be the case here.

In the first line of any method declaration it’s required to state what the type is of what’s going to be returned.

E.g. double increaseAmountBy(double x) is a method that will return a double.
int divisors() will return an int. void display() will not return any value (vacuous return). You can explicity write return inside the void method to clarify that it ends, but it is not required and it cannot return anything other than an empty value.

1 Like