| Author |
Missing return statement. Why an error?
|
Mario Skrlec
Greenhorn
Joined: Apr 15, 2012
Posts: 20
|
|
Hi everyone! This is the problem...
Missing return statement. Why do i have to put a return statement outside of the 'if' statement if there is no possible way of executing excatly that statement? Why is it an error and what is the logic in it?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56191
|
|
|
You don't. Please show us the code that generated the error. It'd be best if it's an SSCCE (Short Self-Contained Correct Example).
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Gaurangkumar Khalasi
Ranch Hand
Joined: Jun 02, 2012
Posts: 186
|
|
In your function of "public boolean setFileName()", Compiler can able to see two statements 1) String declaration and 2) If else statements. And as your function has a valid return type other than "void", compiler try to find third statement i.e. return statement. Compiler do not know about your logic of "If-else", it knows about its semantic validation...
So, you got an error...
|
 |
Gaurangkumar Khalasi
Ranch Hand
Joined: Jun 02, 2012
Posts: 186
|
|
You can write the same function in the following manner also
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56191
|
|
Gaurangkumar Khalasi wrote: Compiler do not know about your logic of "If-else", ... So, you got an error...
Not correct. The compiler knows that one of the if or the else parts must execute. And since each is terminated with a return there is no need for the extra return at the end. In fact, doing so will result in an unreachable code error.
|
 |
Gaurangkumar Khalasi
Ranch Hand
Joined: Jun 02, 2012
Posts: 186
|
|
Bear Bibeault wrote:Not correct. The compiler knows that one of the if or the else parts must execute. And since each is terminated with a return there is no need for the extra return at the end. In fact, doing so will result in an unreachable code error.
Sorry, my mistake
|
 |
Palak Mathur
Ranch Hand
Joined: Jan 29, 2007
Posts: 303
|
|
Gaurangkumar Khalasi wrote:In your function of "public boolean setFileName()", Compiler can able to see two statements 1) String declaration and 2) If else statements. And as your function has a valid return type other than "void", compiler try to find third statement i.e. return statement. Compiler do not know about your logic of "If-else", it knows about its semantic validation...
So, you got an error...
Gaurang, I will beg to differ here. The compiler knows that if 'if-else' statements are present, then one has to be executed and if each has a return statement then there is no need for the return statement outside if-else block.
In case, where you use only if statement, then you will need that extra return statement outside the if-block because in that case compiler doesn't know whether that if statement will actually be executed.
|
Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
|
 |
Mario Skrlec
Greenhorn
Joined: Apr 15, 2012
Posts: 20
|
|
I apologize. I didn't read it before i posted but now i did and hope it fits.
Before i posted, i googled the web for 'missing return statement' and 'missing return statement in if clause' but did not find anything.
Afterwords, i searched the web more thoroughly and found the solution.
The problem was that, if the first 'if' statement is to be executed, then there is no need for 'else' beacuse it executes anyway which is logical but,
if i decide to put an 'else' just to make it clearer, what do i put as a return type for the third solution? I found ERROR but it looks ackward.
Is there a workaround to this?
Thank you for the answers
|
 |
Mario Skrlec
Greenhorn
Joined: Apr 15, 2012
Posts: 20
|
|
On the other hand, this works. Is inheritance the problem? But why would it be?
Thank you for your answers.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56191
|
|
Inheritance? of course not.
It works because there are only two path through the method and each is terminated with a return. Any method that has a path that does not result in a return will be in error.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
In your case, what would happen if this would not be a instance of Reader?
That technique is horrible by the way. A class should not know which sub classes it has. Overriding the method is the way to go.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Mario Skrlec
Greenhorn
Joined: Apr 15, 2012
Posts: 20
|
|
|
Thank you for the answers
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4749
|
|
Mario Skrlec wrote:I apologize. I didn't read it before i posted but now i did and hope it fits.
No probs. Thanks for taking the time.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
 |
|
|
subject: Missing return statement. Why an error?
|
|
|