return statement on a void?

Theres a part that i dont understand, and if its possible i'd like to be helped a bit with it ^_^.

This class is part of a program that uses threading to display a consistent "Tick Tock", but the part that i dont get is why is there a return statement on the method that is on bold, isn't void supposed to mean that it doesnt return a value, is the return statement just used to get out of the if loop?, or maybe something else! help is appreciated!

class TickTock{

[b]synchronizedvoid tick(boolean running){

if(!running){// stop the clock

notify();// notify any waiting threads

return;[/b]

}

System.out.print("Tick ");

notify();// let tock() run

try{

wait();// wait for tock() to complete

}

catch(InterruptedException exc){

System.out.println("Thread interrupted.");

}

}

synchronizedvoid tock(boolean running){

if(!running){// stop the clock

notify();// notify any waiting threads

return;

}

System.out.println("Tock");

notify();// let tick() run

try{

wait();// wait for tick to complete

}

catch(InterruptedException exc){

System.out.println("Thread interrupted.");

}

}

}

Message was edited by:

Rix87

[2638 byte] By [Rix87a] at [2007-11-27 9:27:54]
# 1
Void means that is does not return a value, the return statement is not returning a value, it is returning control back to the calling code. It is similar to break in a loop.
_helloWorld_a at 2007-7-12 22:31:07 > top of Java-index,Java Essentials,New To Java...
# 2

A simple return statement means that you are just exiting that function. A function can use the return statement even if it is declared a void. It is just that from a void function you can not return a value.

In your code the function is doing some condition checking (if statements) and based on that decides whether to exit the function or to continue going forward.

Additionally, the if-construct is not a loop. If statements provide conditional branching mechanism.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/branch.html

You probably will never need to break out of an if statement. If you do need it, then perhaps your if condition is suspect.

http://java.sun.com/developer/TechTips/2000/tt0613.html

You can use labeled breaks if you need to break out of nested if statements but it is very rare that you will need them and the code can perhaps be written in another way which is much more readable than labeled breaks.

DarumAa at 2007-7-12 22:31:07 > top of Java-index,Java Essentials,New To Java...
# 3
Oh alright, thanks a lot!
Rix87a at 2007-7-12 22:31:07 > top of Java-index,Java Essentials,New To Java...