Java – “return” stop executing the method?

I have written a method as follows:

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

In my opinion, the method returns 1 and then executes the rest of the code Really? Do not return to stop the execution of the code It's not, how do I force a way to stop?

add to

This is the code (as required):

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 twice In each case, I generate a log file In both log files, I see the "set (by button)" statement (directly before returning) But the problem is that I see the "timer of" statement in the second log file If "set (button)" is reached, it cannot be reached How did this happen? I need to mention that "set (by button)" and "timer" don't happen anywhere in my code (they only happen once)

Add 3

As you can see from the code, I don't have a finally statement

Solution

This is not true. The return statement will stop any of the following code (the only exception is that the return statement is in the try {} block, followed by a finally {} block

if(0==0){
       return;
    }
    System.out.println("This will not print.");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>