aspose file tools*
The moose likes Beginning Java and the fly likes Error w.r.t return values for a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error w.r.t return values for a method" Watch "Error w.r.t return values for a method" New topic
Author

Error w.r.t return values for a method

Abhishek Mj
Greenhorn

Joined: Jan 07, 2013
Posts: 5


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

Joined: Jan 10, 2002
Posts: 61432
    
  67

This is not good practice:


Consider:


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

I could go on about using numbers to represent days, but that's another show...
Abhishek Mj
Greenhorn

Joined: Jan 07, 2013
Posts: 5
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

Joined: Aug 16, 2005
Posts: 14269
    
  21


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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 147

Or you might be working with some old version of a class-file. Did you try recompiling your code?


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Abhishek Mj
Greenhorn

Joined: Jan 07, 2013
Posts: 5
@Jesper: Thanks a lot for the clear answer!
@ Jan: Yeah. I tried with different inputs and recompilation. But found the same error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error w.r.t return values for a method