Roman
Roman

Reputation: 131138

Does "return" stop the execution of a method?

I have programmed a method in the following way:

if (something) {
   return 1;
}
the rest of the code

It seems to me that the method returns 1 and then execute the rest of the code. Can it be the truth? Doesn't return stops the execution of the code. It it is not, how can I force a method to stop?

ADDED

Here is the code (as requested):

    for (int i=availableTime; i>0; i=i-1) {
            final int sec = i;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    String lbl = "<html>";
                    lbl += "</html>";
                    timeLeftLabel.setText(lbl);
            }
            });
            try {Thread.sleep(1000);} catch (InterruptedException e) {}
            parameterFromClientsListener = clientsListener.getValue(userName,parameterToGet);
            if (!parameterFromClientsListener.equals("null")) {
                output = parameterFromClientsListener;
                game.log.fine(userName + " set (by button) " + parameterToGet + " to be equal to " + output + " . [IMPORTANT]");
                return output;
            }
    }

    game.log.fine("The partner selection phase is expired.");
    // This code is executed if the Submit button was not pressed and the time run out.
    if (parameterToGet.equals("partner")) {
        tellMyChoice(parameterToGet, this.partnerFromForm, "timer of" + field);
        output = this.partnerFromForm;
    }
    game.log.fine(parameterToGet + " was submitted by timer (not by OK button).");
    } else {
    output = parameterFromClientsListener;
    }
    game.log.fine(userName + " set (by timer)" + parameterToGet + " to be equal to " + output + " . [IMPORTANT]");
    return output;
}

I run this code two times. In every case I generate a log-file. In both log files I see "set (by button)" statement (which is straight before the return). But the problem is that in the second log file I do see "timer of" statement. Which should not be reached if the "set (by button)" is reached. How can it be? I need to mention that "set (by button)" and "timer of" do not occur anywhere else in my code (they occur only once).

ADDED 3

As you can see from the code I do not have the finally statement.

Upvotes: 12

Views: 52894

Answers (6)

Roman
Roman

Reputation: 131138

I wanted to understand how the observed behavior can be possible. In more details, I saw a "paradoxical" behavior. In my log files I saw output of the line which happens before the return as well as the output produced by the code after the return. So, I assumed that the return does not stop the execution of the program. As it has been correctly mentioned here by other "answerers" this assumption is wrong. The explanation of the behavior is trivial. My program run the shown code several times. The first time it reaches the return statement, the second time it passes it (because the return is in the if statement). So, it is why IO have the both statements in the log file.

Upvotes: 1

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74760

You write

if (!parameterFromClientsListener.equals("null")) {
    output = parameterFromClientsListener;
    game.log.fine(userName + " set (by button) " + parameterToGet + " to be equal to " + output + " . [IMPORTANT]");
    return output;
}

You are comparing the string (or whatever) with the string "null", and return if they are different. Do you really want to do this, and not parameterFromClientsListener != null?

(Though that should not be a big difference, as long as parameterFromClientsListener is neither null nor "null", and if it is null, your version would give a NullPointerException.)

Do you actually get the entry in your log file? If so, you should also get the return.

Upvotes: 0

jzd
jzd

Reputation: 23629

This is not true, the return statement will stop any following code. (With the only exception being that the return statement is in a try{} block that has a finally{} block afterwards.

    if(0==0){
       return;
    }
    System.out.println("This will not print.");

Upvotes: 14

Nivas
Nivas

Reputation: 18334

Does return stops the execution of the code

well, almost.

Once a return is encountered, the method execution is stopped, and the control is passed to the calling method, after executing any finally clauses.

int add(int a, int b)
{
   try{
      if(a == 0)
      {
         return b;
      }

      if(b == 0)
      {
         return a;
      }

      return a+b;
   }
   finally
   {
      System.out.println("Finally");
   }
}

In the above code, is the function is called as add(0, 1), "Finally" will still be printed.

How can I force a method to stop?

OR What are the other ways of exiting from a method?

Exceptions

Upvotes: 0

jackrabbit
jackrabbit

Reputation: 5663

return does end the execution of the method. There is one exception: the finally block. In the following case, 2 would be returned

public int foo() {
  try {
    return 1;
  } finally {
    return 2;
  }
}

Upvotes: 11

KarolDepka
KarolDepka

Reputation: 8628

Return does indeed end the execution of a method. Check Your other assumptions. Maybe something in other parts of code isn't working as You are assuming.

Upvotes: 0

Related Questions