Skip to content
Advertisement

How to break out or exit a method in Java?

The keyword break in Java can be used for breaking out of a loop or switch statement. Is there anything which can be used to break from a method?

Advertisement

Answer

Use the return keyword to exit from a method.

JavaScript

From the Java Tutorial that I linked to above:

Any method declared void doesn’t return a value. It does not need to contain a return statement, but it may do so. In such a case, a return statement can be used to branch out of a control flow block and exit the method and is simply used like this:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement