File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""return" statement problem" Watch ""return" statement problem" New topic
Author

"return" statement problem

Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Hi,
I would like to know if 'return' statment should be the last statement in a method?
-JEAN
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If the method's return type is something other than void, then a return statement must be at the end of each possible execution path within the method.

For example, if the method includes an "if" statement, then there must be a return statement for both cases -- that is, whether the "if" code executes or not.

If the method's return type is void, then no return statement is needed. (It's implicit at the end of the method body.)
[ April 11, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
I have a callable statement declared locally in a method and I need to close this stament after it executes plus I need to return one of the values from the callable statment. So I need to write something like this :

rs.execute(); //------------->1
rs.close(); //---------------->2
return rs.getInt(1); //------->3


Or, what's the use if interchange the order of 2 & 3? It is wrong right? (my method is returning an int value).
Thanks
-JEAN
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jean Fore:
...what's the use if interchange the order of 2 & 3? It is wrong right? ...

If you exchange 2 & 3, then the code will not compile because the line after the return statement would be unreachable.

I'm not sure what "close" does, but if it cuts off access to the object, then you could store the int value in a temp variable. This would allow you to close before returning from the method.

rs.execute();
int temp = rs.getInt(1);
rs.close();
return temp;
[ April 11, 2006: Message edited by: marc weber ]
Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Thanks Marc,
But after closing the callable statement "rs.close()", can I get values from that 'rs' using "rs.getInt(1)"? The statement is already closed right? That's why I was asking about interchanging the code lines.
Thanks
-JEAN
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
There are two solutions that I can see here. The first is to create a temporary variable that you use to store the value returned by the getInt() method. Then you can call close() and then return the temporary variable. The second is to use a try...finally statement and call close() in the finally clause. This solution uses some knowledge of exception handling and may be more complex than needed. On the other hand, it also ensures that close() will be called whether or not an exception is thrown.

Layne


Java API Documentation
The Java Tutorial
Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Okay!! I will use the finally block and close the statement there.
Thank you so much
-JEAN.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "return" statement problem
 
Similar Threads
Window Closing...
How To Stop the execution of the Action Class
initialising db connection
response.sendRedirect Immediately In Struts
return statement after response.sendRedirect() method?