• 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

Compiler says Missing return statement???

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I tried to write code to test some of the Math methods(), but when I tried to compile, I get a "missing return statement double myMethod" from compiler. Please anyone help

class Testi {
double a = 90.7;
double b = myMethod();
public static void main(String arg[]) {
Testi t = new Testi();
t.myMethod();
}
double myMethod(){
Math.ceil(a);
System.out.println(b);
}
}
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You declare that this method returns a double, don't you? It says "double myMethod()". That tells the compiler, as well as anyone using this class, to expect a double value to be returned when the method ends.
But you're not returning *anything*, let alone a double value.
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your response Rob, would you mind elaborating?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm, if you're asking how a method returns a value then this really belongs in the beginner section.
Anyway, are you familiar with the return statement? It causes control to exit the current method and return to the caller of that method. If you include a value after the return, that value is returned to the caller.
for example...
int anInt = getInt(); //line 1
public int getInt()
{
return 4;
}
After line 1 completes, the variable anInt will contain the int value 4, because that is the value returned from the method call.
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, you're too kind.
 
reply
    Bookmark Topic Watch Topic
  • New Topic