File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finally doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finally doubt" Watch "finally doubt" New topic
Author

finally doubt

xie li
Ranch Hand

Joined: Nov 30, 2005
Posts: 54


the outputis

b
b
bab
ba
who can explain why?

Edited by Corey McGlone: Added CODE Tags
[ January 03, 2006: Message edited by: Corey McGlone ]
brij tiwari
Greenhorn

Joined: Dec 27, 2005
Posts: 2
For the first call with foo(0), the finally block prints the output value then returns the output value which is then printed by the system.out call in main method.so value 'b' is printed two times.

For second call with foo(1),the catch block make output as 'ba' and returns 'ba' to main method, but before returning finally block is called always...so before printing the returned value i.e. 'ba' first finally block prints the 'bab' value...after that since the return value is 'ba' only thats y main prints 'ba'

so the sequence is
b
b
bab
ba
vivekkumar sharma
Ranch Hand

Joined: Dec 21, 2005
Posts: 70
Originally posted by brij tiwari:
For the first call with foo(0), the finally block prints the output value then returns the output value which is then printed by the system.out call in main method.so value 'b' is printed two times.

For second call with foo(1),the catch block make output as 'ba' and returns 'ba' to main method, but before returning finally block is called always...so before printing the returned value i.e. 'ba' first finally block prints the 'bab' value...after that since the return value is 'ba' only thats y main prints 'ba'

so the sequence is
b
b
bab
ba


Why the modification done to output in finally gets lost?
I mean why output is not
b
b
bab
bab
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40

Why the modification done to output in finally gets lost?
I mean why output is not
b
b
bab
bab


The code in the finally clause runs *last* !! It runs after the return value of the method has already been calculated.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306

Why the modification done to output in finally gets lost?

Excellent question. Answer : Because output which is of type String is immutable.

See the code below. Just replaced String with Stringbuffer which is mutable.



Here you will get the following output

b
b
bab
bab


In both code snippets return happens first; then the finally executes.

In the first code snippet the instance of the String referred by variable output changed with each '+' operation.
In the second code snippet the instance of the String referred by variable output didn't change with append.

In the first code snippet, the object referenced by output contained ba and that refrence is returned. Then when finally executed a new String instance is created (because String is immutable) and value of Output is changed to refer to new instance. Hence the print statement from finally had the most recent instance which contained 'bab' and the print statement from main had the second most recent instance which contained 'ba'.

Please let me know if this explanation make sense.
xie li
Ranch Hand

Joined: Nov 30, 2005
Posts: 54
thank u
vivekkumar sharma
Ranch Hand

Joined: Dec 21, 2005
Posts: 70
Hi Jiju Ka and Henery,

Thanks for very good explanation
Vikas Roy
Greenhorn

Joined: Jan 05, 2006
Posts: 2
Hi Ranchers,

I have one doubt about the given explanation.
How come the catch block construct the String as " ba", actually it is only appending "a" in output object.
I have not tried the code, but according to me the output should be " ab" becouse whenever try executes and throws some exception, catch block executes before finally block.

Please do correct me or explain me.

Regards,


Vikas Chandra Roy
sivaram omprakash
Greenhorn

Joined: Jan 03, 2006
Posts: 5
But we have assigned the modified string to output reference variable in finally block.so why doesnt work?
venkataramana raju
Greenhorn

Joined: Sep 11, 2005
Posts: 28
Hi Vikas Roy ,

quote:
----------------------------------------------------------------
Originally posted by Vikas Roy :

How come the catch block construct the String as " ba", actually it is only appending "a" in output object.

----------------------------------------------------------------
why because output variable is static (class variable)
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Originally posted by Vikas Roy:
Hi Ranchers,

I have one doubt about the given explanation.
How come the catch block construct the String as " ba", actually it is only appending "a" in output object.
I have not tried the code, but according to me the output should be " ab" becouse whenever try executes and throws some exception, catch block executes before finally block.

Please do correct me or explain me.

Regards,


Field output has a static modifier, after first invoking of method foo() String output is set to "b", catch block add "a" and finally block adds "b", so output is "bab".
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
Vikas Roy ,
Your question

How come the catch block construct the String as " ba", actually it is only appending "a" in output object.


Is answered below.
From the method main the method foo is called twice. In the first call '0' is passed as argument which will not get into exception. When finally executes variable output gets a value b. In the second call to foo '1' is passed as argument which will get into an exception. Since output is static variable both calls to foo is accessing the same string referenced by output. When catch executes "a" is appended to output which already have "b". Try debugging this program using eclipse, netbeans, etc.. for more details
 
jQuery in Action, 2nd edition
 
subject: finally doubt
 
Similar Threads
Exception
Interface question from JQPlus
exception..try catch block execution
Exception
exception handling question