Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return Statements

 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mahesh Pinnamaneni
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Mahesh
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic