-1
public void warning(){
    System.out.println("Watch out!");
}

public void warning(){
    System.out.println("Watch out!");
    return;
}

We must use "return" statement with void methods. Bot of these bring same output. What's the difference?

1 Answer 1

13

You needn't use return with void methods in Java, although some coding conventions may call for it, it is not a requirement of the language. Semantically they behave exactly the same way, as the "end" of a void method is an implicit return.

Now, it is also possible to put a return; in a conditional block (if, for, while, etc...) where it might exit the method early. For example:

void someMethod() {
   if (this.avoidSomething) {
       return;
   }
   System.out.println("Something not avoided.");
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.