GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Missing Return Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Missing Return Statement" Watch "Missing Return Statement" New topic
Author

Missing Return Statement

C Hurd
Greenhorn

Joined: Feb 21, 2009
Posts: 2
My code is below... when I compile, it gives me a "Missing Return Statement" on my middle method. I realize that it's due to it being Double instead of Static... I don't fully understand Method openings and their impact. This is like my first week of playing with Java. :-/

I am bolding the method that is throwing the compilation error.

Thanks!!

[edit]Add code tags. CR[/edit]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
Welcome to JavaRanch

As you remember, a method heading has several parts (you can find full details in the Java Language Specification but it's not easy to read):
  • Access modifier (optional): private, public or protected.
  • Other modifiers (optional), eg static final, synchronized.
  • Generic formal type parameter (optional) eg <T>, and I think that comes here rather than later.
  • return type (compulsory). If the method returns nothing, write void instead of the return type.
  • method identifier (compulsory)
  • (
  • List of parameters, separated by commas. A zero-length list is permitted
  • )
  • In the example you quoted, the method declares a return type of "double." This tells the compiler that a double value will come back when that method completes. You should be able to write

    double sum = getSum(numbers);

    So the compiler goes through every method and looks whether it returns what it claims to, from every possible "path" through the method.
    Your method claims to return a double, but doesn't return a double. It actually behaves as a method which doesn't return anything (see above). It also has the peculiarity that it claims to take a double[] as a parameter, but it doesn't do anything with that parameter. It calculates and prints the total of an int[]. So you have an unused parameter; that ought to be deleted.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39079
        
      23
    Please use the code button; I have edited your post so you can see how much better it looks.
    You can find the Java Language Specification section here, but it is by no means easy to read.
    C Hurd
    Greenhorn

    Joined: Feb 21, 2009
    Posts: 2
    Thank you so very much!

    Be warned that I will be back with many many questions .
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39079
        
      23
    You're welcome
     
    GeeCON Prague 2014
     
    subject: Missing Return Statement