• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Missing return statement. Why an error?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can write the same function in the following manner also


 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the answers
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic