Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error w.r.t return values for a method

 
Abhishek Mj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Even though I have given return statements, got error of methods not returning.. Can you please explain?



Error Msg:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type boolean

at SleepIn.whenSleepIn(SleepIn.java:10)
at SleepIn.main(SleepIn.java:25)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not good practice:


Consider:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could go on about using numbers to represent days, but that's another show...
 
Abhishek Mj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I am trying to understand how the compiler working on the code and displaying the error. Can you provide more information on how to use return statements with if statements?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15437
41
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The compiler isn't smart enough to see that your two if-statements cover all possible cases. It's complaining that there might be a path through the method for which there is no return value specified.

The compiler checks your code, but the checks it can do are limited. Remember that it's just a program, not a human being that can think and reason itself out of any arbitrary complex situation.

*edit* I just thought of a possibility. Suppose that the variable weekday is being modified by another thread at the same time the above code is executing. The following might happen: The first if-statement is evaluated and it's false, so we go on to the second if-statement. At that moment, another thread modifies weekday, so that also the second if-statement evaluates to false. Now the method reaches a point where the return value isn't specified.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you might be working with some old version of a class-file. Did you try recompiling your code?
 
Abhishek Mj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jesper: Thanks a lot for the clear answer!
@ Jan: Yeah. I tried with different inputs and recompilation. But found the same error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic