aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "understanding return types .." Watch "understanding return types .." New topic
Author

understanding return types ..

Nick Faiz
Greenhorn

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() ;
}
}
Thanks,
biv
[ January 29, 2003: Message edited by: brain_in_vat ]
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy!
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) {
doStuff();
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?
cheers,
Kathy


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />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
Sheriff

Joined: Aug 26, 2001
Posts: 7610
brain_in_vat,
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!


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Nick Faiz
Greenhorn

Joined: Oct 12, 2002
Posts: 11
Kathy,
Thanks for your answer!
To hazard a guess, for an answer to your question -
// pseudocode
try
{
...
return ;
...
}
catch (SomeException e)
{
return ;
...
}
finally
{
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: http://aspose.com/file-tools
 
subject: understanding return types ..
 
Similar Threads
concatenation
static method invocation problem
main() is the entry point
Method Overloading with default param values