Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"return" statement problem

 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would like to know if 'return' statment should be the last statement in a method?
-JEAN
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay!! I will use the finally block and close the statement there.
Thank you so much
-JEAN.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic