Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes understanding return types .. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "understanding return types .." Watch "understanding return types .." New topic

understanding return types ..

Nick Faiz

Joined: Oct 12, 2002
Posts: 11
This is an oddity I don't quite understand.
Can anyone shed some light on why foo(), here, which has no return type whatsoever (i.e. void) operates quite well with an empty return statement?
If it seems odd, I am using software that specifies methods be written as foo() is.

public class TestingReturnType {

public final static void foo()
System.out.println( 1 ) ;
return ;

public static void main( String[] args )
foo() ;
[ January 29, 2003: Message edited by: brain_in_vat ]
Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
Good question. The 'return' keyword means 'return from this method NOW (popping the method off the stack and returning to the calling method)'. If there *is* a non-void return type, the return MUST be followed by a value or variable that can be legally assigned to the return type. (return 2; return new Dog(); etc.)
So in the code that you have, you're right -- the return statement has no purpose since that's the end of the method anyway. But... you *could* do something like:
void myMethod() {
if (x > y) {
return; // says get out NOW!
// continue on
// more code
In this context (with a void return type), the 'return' is kind of like a 'break from the whole method' statement.
Of course, this isn't necessarily a good coding practice, but that's a different story...
Bottom line: you can always have a return statement in a method. In a method with a non-void return type, the return MUST be followed with a value/variable that is compatible with the return type of the method.
In either case, return ALWAYS means "get out of the method now and return control to the calling method'".
Except... could there be a case where a return will NOT immediately take you out of the method�where the current method stays on the stack for just a little while longer before the actual 'return' happens?
I'll leave that as an exercise for those of you just starting your studies
So that's the question:
Under what circumstances, if any, does a 'return' statement not cause the current method to pop off the stack immediately?

Co-Author of Head First Design Patterns
Just a Jini girl living in a J2EE world.
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
to have a go at your question Kathy: when a return statement indicates something like
return new Dog();
and the constructor for Dog has a couplke of method calls within it that holds things up for a while?

giddee up
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!

[Blog] [Blogroll] [My Reviews] My Linked In
Nick Faiz

Joined: Oct 12, 2002
Posts: 11
Thanks for your answer!
To hazard a guess, for an answer to your question -
// pseudocode
return ;
catch (SomeException e)
return ;
System.out.println( "!" ) ;
I'm a little intrigued - where can I learn about the stack that contains currently executing methods - the JLS?
I'm actually a SCJP and am beginning study for the SCWCD quite soon. It's great to learn more about the fundamentals, however.
[ January 30, 2003: Message edited by: Nick Faiz ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
There is a chapter of Inside The Java 2 Virtual Machine and from Richard G. Baldwin
Processing stack trace in Java

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: understanding return types ..
It's not a secret anymore!