Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...