I've looked at other questions and still can't figure it out. Why won't it let me compile this code with a switch statement? I get error that typical error "case expressions must be constant expressions". I am trying to switch on byte from a message. I want to use the switch due to speed issues and try not to do any conversions i.e. from int to byte. My Utils class contains an enum PID with A,B,C...in it. I want to switch on these but the message I get back is in bytes.
public class SomeClass extends Thread {
public static final byte myCase1 = (byte) Utils.PID.A.ordinal();
public static final byte myCase2 = (byte) Utils.PID.B.ordinal();
public static final byte myCase3 = (byte) Utils.PID.C.ordinal();
private double[] findAllData(ByteBuffer message) {
byte[] byteBuffer = new byte[9000];
// parse through and find all PIDs
for(int i=0 ;i < message.capacity(); i++) {
message.position(i);
switch (message.get(i)) {
case myCase1 : break; // Compiler errors at the case statements
case myCase2 : break;// Compiler errors at the case statements
case myCase3 : break;// Compiler errors at the case statements
}
}
}
// Utility class
public class Utils {
public enum PID { A,B,C };
}