For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2004 > return in void method









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author return in void method
Petterson Mikael

2004-07-21, 3:58 am

Hi,

I was looking at some code on sun's site
(http://java.sun.com/docs/books/tuto...ientApplet.java).

Specifically these lines (see below)

Why is there a return in a void method?

//Mikael


====================================

synchronized void waitForTryst() {
//Wait for notify() call from action().
try {
wait();
} catch (InterruptedException e) {}

if (DEBUG) {
System.out.println("waitForTryst about to return. "
+ "trysted = " + trysted + ".");
}

return;
}

Murray

2004-07-21, 3:58 am


"Petterson Mikael" <mikael.petterson@ericsson.se> wrote in message
news:40FE06A1.7090907@ericsson.se...
> Hi,
>
> I was looking at some code on sun's site
>

(http://java.sun.com/docs/books/tuto...mple-1dot1/Talk
ClientApplet.java).
>
> Specifically these lines (see below)
>
> Why is there a return in a void method?
>
> //Mikael
>
>
> ====================================
>
> synchronized void waitForTryst() {
> //Wait for notify() call from action().
> try {
> wait();
> } catch (InterruptedException e) {}
>
> if (DEBUG) {
> System.out.println("waitForTryst about to return. "
> + "trysted = " + trysted + ".");
> }
>
> return;
> }
>


That particular return statement is redundant, but some people choose put it
there as they feel it improves code readability. Personally, I don't see any
benefit.

Of course, it is perfectly valid if the return was somewhere else in the
method, e.g.

if (someCondition) {
return; // exit the method
else {
keepDoingOtherStuff ...
}


Roedy Green

2004-07-21, 3:58 am

On Wed, 21 Jul 2004 07:01:09 GMT, "Murray"
<parps@SMAFFoffSPAMMER.optusnet.SPAMMAGE.com.au> wrote or quoted :

>
>That particular return statement is redundant, but some people choose put it
>there as they feel it improves code readability. Personally, I don't see any
>benefit.


If there was an early return in the method, the second return at the
end just emphasises the parallelism, and hints there is another to
watch out for. Normally you would leave that last one out.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Mark Haase

2004-07-23, 3:58 pm

In article <40FE06A1.7090907@ericsson.se>,
Petterson Mikael <mikael.petterson@ericsson.se> wrote:

> Why is there a return in a void method?


"return" isn't used just for returning values to the calling code, it
also returns control of execution to the calling code. A compiler will
generate the proper code to return at the end of any void function, but
sometimes you may want to jump out of the method earlier, eg. if you
detect an error.

--
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com