|
Re: Switch statement and case variables
|
Posted: May 16, 2002 6:12 AM
|
|
Yes, you can initialize blank final variables, which have been declared as intance varaibel of a class, in construtors as in the following example. Still, you cannot use that variable in case of switch-case because it is not a compile time constant.
/////////////////////////////////
public class Switchtest {
final int x;
Switchtest ( ) {
x = 10 ; // Inittialize x to 10
}
Switchtest ( int a ) {
x = a ; // Inittialize x to a
}
public static void main(String[] args) {
Switchtest s = new Switchtest ( ) ;
}
public void demo() {
int test = 0;
switch (test) {
case x: // Still it is an error to use blank final variable x
System.out.println("case x " );
break;
default:
System.out.println("Default ");
break;
}
}
}
|
|