Heres my simple code for testing purposes.
boolean isMoving(){
if (a == b) {
return true;
} else if (a != b) {
return false;
}
}
I get the error:
This method must return a result of type boolean
While at any given time it must either return true or false, and never returns both. Also, no matter what I am doing, Eclipse tells me
Cannot make a static reference to the non-static field bikeSpeed
for
void speedUp(){
bikeSpeed++;
}
with bikeSpeed being
int bikeSpeed = 2;
Probably simple answer, but could anyone help? I know how to use booleans and such, and have never had a problem, but the simplest of things ^^^ is not working.
EDIT: Heres my speedUP:
void speedUp(){
bikeSpeed++;
}
And heres my usage of it:
System.out.println("Simple if && if-then-else testing.");
if (isMoving == true) {
System.err.println("You're already moving fast, no need to speed up!");
} else {
speedUp();
System.out.println("Time to speed up!");
}
EDIT AGAIN: The isMoving declaration and such is fine now, but the line if (isMoving == true) { gives the error: isMoving cannot be resolved to a variable. So does isMoving() if i switch it to that. The last error is
speedUp();
gives: Cannot make a static reference to the non-static method speedUp() from the type HelloWorld
EDIT lAST TIME: I hate this fix, since it doesnt seem correct to me, but eclipse seems to think that making EVERY SINGLE VARIABLE static fixes it (which it did) even though it wasnt used statically.
boolean isMoving(){
if (a == b) {
return true;
} else if (a != b) {
return false;
}
}
The compiler here is not detecting that one of those two branches must be hit, and therefore it thinks that there is a possible state where you would get to the end of the method without specifying a return value. You can use else instead of else if or just return a == b.
That's not just a compiler deficiency; you could have a scenario where another thread is changing the values of a and b in the background, and depending on the timing both of those conditions might be false when they are evaluated.
This is equivalent:
boolean isMoving(){
return a == b;
}
Eclipse is complaining because it thinks that not all execution paths are handle. Try:
boolean isMoving(){
if (a == b) {
return true;
} else if (a != b) {
return false;
} else {
return //a is neither equal nor not equal to b???
}
}
Note that the last branch can actually never happen because if a == b condition is not met than a != b must be true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With