aspose file tools*
The moose likes Java in General and the fly likes Doubt in try and finally block execution. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Doubt in try and finally block execution." Watch "Doubt in try and finally block execution." New topic
Author

Doubt in try and finally block execution.

Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162
Hi All,

i am not able to understand this program.

i expected null in SOP of main method.

but, it prints Code Ranch.



Please Help me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38348
    
  23
It won't compile because you haven't declared i.
It doesn't print out CodeRanch. It prints three lines. Please give us full details and explain what you don't understand.
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300


Considering you will manage to compile this code somehow eg. removing or declaring variable i , when we run this program we get the following output
test 1Code Ranch
test 2null
Test:Code Ranch


What does it mean...Simply that method test is run before the main method. See whenever you have static method or block in your class it is loaded first whenever the class is loaded by jvm. So In this case static method test is run before the main method.

I hope it will clear your doubt.

Thanks,
Patricia
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Patricia Samuel wrote:What does it mean...Simply that method test is run before the main method. See whenever you have static method or block in your class it is loaded first whenever the class is loaded by jvm. So In this case static method test is run before the main method.


I'm afraid that's wrong. Only static blocks are run when the class is loaded. Static methods only run when they are called. In this case the test() method is being called from this line in the main method


Joanne
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Yeah Neal... you're right. i made a mistake in writing the post.

Thanks for correcting me.
Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162
i have updated the code.

please take a look and let me know the reason why its printing Code Ranch.

if java return the final value for the variable inside try block, then there is no meaning of nullifying variables inside finally block.

Thanks In Advance.

Bennet Xavier X.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Bennet Xavier wrote:i have updated the code.

please take a look and let me know the reason why its printing Code Ranch.

if java return the final value for the variable inside try block, then there is no meaning of nullifying variables inside finally block.

Thanks In Advance.

Bennet Xavier X.


The value returned from the method will be the value of the object that str referenced at the point that the return statement was executed. Making str refer to a different object or null in the finally block will have no effect on the value returned. The only way that the return value can be changed in the finally block is if there is a return statement in there as well.
Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162
Thank You Very Much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in try and finally block execution.