DeferredReturn

Misconception:

A return statement that is not at the end of a method does not immediately return, but it first lets the method finish before returning.

Incorrect

A return statement in the middle of a method doesn't return immediately

Correct

A return statement immediately returns from the method

Correction
Here is what's right.

A return statement returns immediately.

For example:

public void tick() {
 if (value<=0) {
  return;
 }
 value--;
 if (value==0) {
  ringBell();
 }
}

In the above code, if value<=0 then the return statement executes and returns from the method immediately (which means that in this case the subsequent lines are not executed).

Note for advanced students: The only situation where return does not return immediately is if the return statement is located inside a try block with a corresponding finally block.

Value
How can you build on this misconception?

This misconception can provide a good opportunity to discuss statement sequences as a key aspect of imperative programming.

This misconception can be an opportunity to point out the fact that return statements located inside a try block with a corresponding finally block indeed do not return immediately. However, a full-fledged discussion of the details of exception handling may be too advanced at the time students hold this misconception.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.