File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "return statement" Watch "return statement" New topic

return statement

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
If I remove the return statement in //1 it gives compileerror saying no return statment..

Why do we need return in it ? What are the rules for using it....tofind out whether the code is error or not


public class Ppvg{
public static void main(String argv[]){
Ppvg p = new Ppvg();
public int fliton(){
FileInputStream din = new FileInputStream("");;
}catch(IOException ioe){
return 99;
return -1;

achayya matta
Ranch Hand

Joined: Oct 27, 2005
Posts: 111
hai karthik
tell me which line exacty.u were saying "//1" , i did nt find it in your code.
Mausam M Kakkad
Ranch Hand

Joined: Oct 27, 2005
Posts: 40
Hi Karthik,

I assume it is the last "return"(at the end of the method) you are asking about.

In that case, "The method has to return something if its return type is not "void" ". and here "something" depends on what return type is.

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
ho sorry

the last return type one
Amar Prasad

Joined: Sep 28, 2005
Posts: 5

you have first return i.e. return 99; in catch block. So it will execute iff there is any IOException.

So you need to write return -1; as the last statement of the method.

if there is any IOException your return 99; will be executed followed by finally block. else second return statement retrun -1; will be executed.
I agree. Here's the link:
subject: return statement
It's not a secret anymore!