Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch case with 'static final byte'

I need to use switch case with byte contants. I have static final constants declared like follows:

private static final byte[] X_CONST = {2};
private static final byte[] Y_CONST = {3};

Then I want to use switch case like follows :

byte[] x={3};

switch (x[0]){
    case X_CONST[0]: ...; break;
    case Y_CONST[0]: ...; break;
}
like image 202
Rajat Gupta Avatar asked Dec 21 '22 07:12

Rajat Gupta


1 Answers

The array may be static final, but the content of the array isn't. So it isn't allowed as the case value of a switch as the value itself could be changed at runtime. You will need to specify private static final byte X_CONST = 2 instead.

like image 176
Mark Rotteveel Avatar answered Dec 24 '22 02:12

Mark Rotteveel