So after compile-time in the bytecode there is no variable "month" in the memory and "5" is placed in the places where "month" was?
Yes and no. What is happening is the final
keyword tells the compiler the variable cannot ever change, so it can be treated as a constant and used in places where a constant is required. How this happens under the covers is immaterial, but in practice the constant value will be substituted.
Now let's consider your code:
int month = 5;
switch(month) {
case 1: doSth() break;
...
case month: doSth() break; // compilation error
There's something not right here, because your switch variable is the same as one of your cases. The only possible path through this is the month
case, so all the other cases are dead code. Maybe you meant:
int monthOfInterest = 5;
switch(month) {
case 1: doSth() break;
...
case monthOfInterest: doSth() break; // Still a compilation error
This looks a little better but won't compile since monthOfInterest
is not a "constant expression". Here we get to your original question. Case selectors have to be either numeric literals or expressions that can be evaluated at compile time. So we change this to:
final int monthOfInterest = 5;
switch(month) {
case 1: doSth() break;
...
case monthOfInterest: doSth() break; // This is valid code
This "behaves" as if monthOfInterest
was replaced by the constant 5
. Note that this is not the same as:
final int monthOfInterest;
...
monthOfInterest = 5;
switch(month) {
case 1: doSth() break;
...
case monthOfInterest: doSth() break; // Oops, compilation error again
It is perfectly legal to declare a variable final
and initialize it later. The compiler will ensure you set the value only once, but in this case monthOfInterest
is no longer a constant expression, and this code will not compile.
One final note. consider:
final int monthOfInterest = 1;
switch(month) {
case 1: doSth() break;
...
case monthOfInterest: doSth() break; // Oops again, different compilation problem
This fails to compile since now you have two cases with the same value.