I have the following:
public class Asd
{
static final String VERSION = "version";
static final String ECHO = "echo".toUpperCase();
public Asd()
{
String key = ECHO;
switch (key)
{
case VERSION:
break;
case ECHO:
break;
}
}
}
I get a compiler error that case ECHO:
is not a constant expression. The designation final static
defines a constant.
The constant ECHO
will resolve from "echo"
to "ECHO"
when the class is instantiated. By the time the switch/case is evaluated, ECHO
does equal "ECHO"
and cannot be changed.
Or does a switch/case with String have some special background pointers to a String?
Because using
public class Asd
{
static final int VERSION = 1;
static final int ECHO = 1 + 2;
public Asd()
{
int key = ECHO;
switch (key)
{
case VERSION:
break;
case ECHO:
break;
}
}
}
Is error free even though 1 + 2
must be evaluated.