I don't agree with you Vinit, the finally block will be executed in all
cases. but in this case, which the try block execution is going well the
finally block will be executed after calculating the return value.
I hope this sample can make it more clear what is going on exactly; it will
print 35 then 12.
static int dist = 0;
public static int getresult() {
try {
int result = -1;
dist = 35;
result = dist;
System.out.println("should return..");
return result;
} finally {
System.out.println("Execute finally first, but the return value
has been calculated!");
dist = 12;
}
}
public static void main(String args[]) {
int j = getresult();
System.out.println("Result = "+j);
System.out.println("dist = " + dist);
}
----
Regards,
Ahmed Hashim
On Mon, Mar 22, 2010 at 4:13 PM, vinit sharma <[email protected]> wrote:
> This is because when the loop encounter return key word it just out from
> there and no further statement is executed in that loop, In your case it
> return 35 and no finally block is executed.
>
>
> On Mon, Mar 22, 2010 at 5:03 PM, vinay basavanal <[email protected]>wrote:
>
>> Hi all,
>>
>> I want explanation for the output of following programme
>> ,the output is 35, but i was expecting it to be 10 since finally gets
>> excuted at the end
>>
>> public class testfinal {
>> static int dist=0;
>>
>> public static int getresult(){
>> try{
>> dist=35;
>> return dist;
>> }
>> finally{
>> dist=12;
>> }
>> }
>>
>> public static void main(String args[]) {
>> int j=getresult();
>> System.out.println(" dist " + j );
>> }
>>
>> }
>>
>> --
>> To post to this group, send email to
>> [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]<javaprogrammingwithpassion%[email protected]>
>> For more options, visit this group at
>> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>>
>> To unsubscribe from this group, send email to javaprogrammingwithpassion+
>> unsubscribegooglegroups.com or reply to this email with the words "REMOVE
>> ME" as the subject.
>>
>
> --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]<javaprogrammingwithpassion%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>
> To unsubscribe from this group, send email to javaprogrammingwithpassion+
> unsubscribegooglegroups.com or reply to this email with the words "REMOVE
> ME" as the subject.
>
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to this email
with the words "REMOVE ME" as the subject.