File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes When A Method Returns? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When A Method Returns?" Watch "When A Method Returns?" New topic

When A Method Returns?

ernest fakudze
Ranch Hand

Joined: Aug 27, 2001
Posts: 216
What does it mean to say 'when a method returns'? I think i have a vague idea but maybe someone can clarify this once and for all for me. Thanks in advance.

In a time of drastic change it is the learners who inherit the future. The learned usually find themselves equipped to live in a world that no longer exists.<br />Eric Hoffer
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
When you call a method, program execution leaves the caller, and goes to that method. At some point, the method will finish, and then control "returns" back from where it originally came.
for example, two methods:
public void callingMethod()
doMethodCall(); //at this line, program execution "jumps" to the doMethodCall() method
public void doMethodCall()
//when there are no more method statements, the method is done, so it returns. In this case, it returns back to the callingMethod(), since that is the method that called this one.
//You can also return explicitly, with a return statement:
return; //this immediately returns control to the caller.
If you have a method that declares that it returns a value, you put the value you are returning to the right of the return statement.
public int howManyFingersOnOneHand()
return 5; //control returns , AND the value 5 is returned to the calling method.
if I called the above method like so:
int fingerCount = howManyFingersOnOneHand();
the value 5 is returned from the method, and stored in the variable fingerCount.

I hope this helps!

SCJP 1.4
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Maybe instead of using the phrase 'when the method returns' think of it as 'when the method is done executing'.

Please ignore post, I have no idea what I am talking about.
ernest fakudze
Ranch Hand

Joined: Aug 27, 2001
Posts: 216
Thanks you so much for explaining this to me. It's that simple! Now I know and I'm happy . Really appreciate guys!!
I agree. Here's the link:
subject: When A Method Returns?
It's not a secret anymore!