Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Missing return statement. Why an error?

 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 65337
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write the same function in the following manner also


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65337
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 65337
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the answers
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic