What happens if we execute the program?class Return{ int display(){ try{ return 1; } finally{ return 2; }}public static void main(String args[]){ Return r=new Return(); r.display();}}when i executed i got the compile time error when i executed on jdk 1.4?but in khalid moghal book its executed and result is printed.

Questions by sadashivarao   answers by sadashivarao

Showing Answers 1 - 11 of 11 Answers

Sandeep

  • Aug 13th, 2006
 

Hi friend, I guess you are new to Java. The problem you quoted is not a problem and its written pricisely in KHALID MUGHAL. No compile time error is generated and just try this version. You will know the result ::


class Return{

int display()

{

try

{

return 1;

} finally

{

return 2;

}

}

public static void main(String args[])

{

Return r=new Return();
System.out.println(r.display());
}

}

Chandu

  • Aug 14th, 2006
 

Display method will return the value 2. Am I correct?

  Was this answer useful?  Yes

purandhar

  • Aug 17th, 2006
 

Hey, when u r returning int or any type there should be a variable to take that int or any other vlue,or we r able to display directly using System.out.println statement.

  Was this answer useful?  Yes

Saroj

  • Aug 22nd, 2006
 

Yes u r correct. Even if there is return statement in try block. finally block will get executed and the output will be 2.

  Was this answer useful?  Yes

harsh123

  • Aug 24th, 2006
 

no, u r wrong , it produce no out put . Regards harsh nigam

  Was this answer useful?  Yes

ganesh

  • Aug 30th, 2006
 

Program compiles doesn't give any error.Purandhar is correct.Sandeep program result is 2

hi you go through functions and how it return values first. 

  Was this answer useful?  Yes

Pallu

  • Sep 5th, 2006
 

You are right Sandeep.

But I guess, Sadashivrao did not set a classpath if he is executing this program on command prompt.

You can set classpath by:(if u saved Return.java in temp folder)

C:Documents and Settingsx.yDesktoptemp>set classpath=%classpath%;
C:Documents and Settingsx.yDesktoptemp

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions