• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

returning a value from a method

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I have an interesting question..maybe not that tough..



If I execute the code as above, I get the output as:

Check #1:11
Check #2:12
Check #3:13
Check #4:12

So as per that, even if the finally block executes first, the value returned back is the one that was there in the catch block. So is there any kind of "marking" mechanism, so that the variable value that was there at the time return was called is actually returned, even though there are manipulations to it later down rhe code-flow?

Thanks,
Kamal
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kamal,


Remember that the finally block is always executed whether the exception is thrown or not whether there is a return in a catch block or anything else nothing can stop execution of finally block. There is only one exception to this. If system.exit() is called within the try/catch block, finally block is not executed. Some refreshing concepts from K & B chapter 5.

Now coming to your question why it prints 12 within the main() function and in fact it changed the value of i in finally clause. This is because whatever int value you are returning from the static testMethod() is pushed into the stack (Remember the value and not i) and it is 12 and then it runs finally (some sort of completing the unfinished business) and then come back and pop the value back and return to the main routine from where it was invoked and that how you got 12.

I hope you got it. If not feel free to ask more.

Regards
Padma
 
Rajiv Arora
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Padma !! I thought so that the variable value is either marked or put on the satck for being popped up later, but wasn't really sure. But your reply confirms it

Kamal
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic