Source :
SCJP Java 2 by Kathy Sierra & Bert Bates.
import java.io.*;
public class Switch{
public static void main(
String args[])
{
final int one =1 ;
final int two = 2 ;
int x = 1;
switch(x)
{
case one :
System.out.println("one");
break ;
case two :
System.out.println("two");
break ;
}
}
}
When I compiled the above program it compiled and executed correctly with an output
one.
The above code uses final variables in a
case statement . Note that if the
final keyword is omitted , the above will not compile.
I understood that if we omit the final keyword , this will not compile but I did not understand why it compiled and executed properly if we add the final variable
. Can anyone please explain it in detail ?
Thanks in advance .
[ September 11, 2007: Message edited by: Rakesh Yelugoila ]