I have the following class:
public class NewGameContract {
public boolean HomeNewGame = false;
public boolean AwayNewGame = false;
public boolean GameContract() {
if (HomeNewGame && AwayNewGame){
return true;
} else {
return false;
}
}
}
When I try to use it like so:
if (networkConnection) {
connect4GameModel.newGameContract.HomeNewGame = true;
boolean status = connect4GameModel.newGameContract.GameContract();
switch (status) {
case true: break;
case false: break;
}
return;
}
I am getting the error:
incompatible types found: boolean required: int on the following
`switch (status)` code.
What am I doing wrong?
You can’t switch on a
boolean(which only have 2 values anyway):The Java Language Specification clearly specifies what type of expression can be
switch-ed on.JLS 14.11 The switch statement
It is a lot more readable and concise to simply use an
ifstatement to distinguish the two cases ofboolean.