aspose file tools*
The moose likes Beginning Java and the fly likes calling 'return' doesn't seem to work immediately? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling Watch "calling New topic
Author

calling 'return' doesn't seem to work immediately?

Max Simpson
Ranch Hand

Joined: Sep 23, 2004
Posts: 30
I have a method and inside of an if/then, I have it print the word "done" and then I call return. Problem is that is prints "done" about 100 times instead of exiting the method right away. It does eventually work, but why does it print "done" so many times first? It almost looks like a loop...
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
it does sound like a loop, yes. could you post the code?
Max Simpson
Ranch Hand

Joined: Sep 23, 2004
Posts: 30
doesn't calling 'return' cause the method to exit immediately, regardless of whether or not it is called from within a loop?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

I too, would like to see the code for this.

But to answer your question... there is a case, where it doesn't return immediately. If there is a finally clause, that code needs to be executed.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

AS Henry says, there is a case where calling return does not exit the function right away. However, in all other cases, return returns, terminating the flow of the method -- even inside a loop.

In order to accurately explain what is going on, we will need to see your code, butI think that you have a loop somewhere that you are not accounting for.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Originally posted by Max Simpson:
doesn't calling 'return' cause the method to exit immediately, regardless of whether or not it is called from within a loop?


As others have said, there are cases where "return" doesn't return right away. But I doubt that's the problem here.

It sounds like you might be confused on what "return" returns from. It will cause the method to exit, but it's the method that the return is in. In other words, your function that prints "Done" is what the return will exit from. And when it exits, it winds up right back at the loop your function was called from. That method (the one with the loop) is not at all affected by the return that's in your "Done" method.

- Jeff
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
As the others have said, posting code snippets of the loop and of the method that has the return statement would help us to help you.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Please post the exact code that is causing this behavior. Without the exact code, we can only guess at what is happening here.

Layne


Java API Documentation
The Java Tutorial
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: calling 'return' doesn't seem to work immediately?