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 Return Statements 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 » Java » Beginning Java
Bookmark "Return Statements" Watch "Return Statements" New topic

Return Statements

Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
Well, "It's been a long time since I rock and rolled", as the song goes. So after a lengthy hiatus, i'm back. Full of cobwebs.

I got as far as completeing and having marked, the 4th (or 5th?) Java assignment.

So i'm trying to pick up where i left off.

Anyway, i've a problem folks, it concerns (I think) the use of the "Return" word.

Below is a short program i've copied out of a book:-

What I don't understand is, in the main class, I call the method "setDim", like so.....

mybox1.setDim(10, 20, 15);

And this method takes 3 parameters, right?

Well, after that, the method "volume" is called on. OK.

From that point on, how do we then manage to arrive back in the main class

Any help would be greatly appreciated. Cheers in advance

John Bonham was stronger, but Keith Moon was faster.
Mahesh Pinnamaneni
Ranch Hand

Joined: Jul 02, 2004
Posts: 100
hai steve,
actually i am not getting what r u talking abt. This statement
stmt1: vol = mybox2.volume();
stmt:2 System.out.println("Volume is " + vol);
} // end of main
gets executed in the main itself. the control will be transfered automatically from the method 'volume()' in Class Box to the stmt1 and is assigned to vol and next the println stmt gets executed and hence it reaches the end of main method, it terminates.and if u write any code like another call to volume method after stmt2 then that will execute and gets terminated.
hope this clears ur doubt.if not feel free to ask me.
cheers & regds,
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I think you are asking how a method knows where to return to.

Basically, the process of calling a method includes saving the return address in a work area called a "stack frame". Any return statement will retrieve the return address from the stack frame and go there.

There is a lot more to the call/return process, but this is what you asked.

Mike Gershman
SCJP 1.4, SCWCD in process
I agree. Here's the link:
subject: Return Statements
jQuery in Action, 3rd edition