Hi everybody
What is the differnce between the following statements :
final int a=1 ;
&
final int b;
b=2;
in context of the following code :
public class A
{ public static void main(
String[] args)
{ final int a=1 ;
final int b;
b=2;
int x=0;
switch(x)
{case a :
case b :
}
}
}
Compiling this code gives the following error on the line "case b" :
constant expression required .
This example is from the kathy and berts book . It says that the case argument has to be a compile time constant . Please explain why "b" is not a compile time constant , despite being marked as final .
And a few additional doubts too.
> Since we have to initialize local variables when they are declared , why is initialization not needed in case of "b" . (Is it because of the final keyword ? )
> I further added the lines a++ and b++ . In the previous case the error was "cannot assign a value to the final variable a " , which is ok . But in the latter case the compiler returned "variable b might already have been intialized " . Why is the error different when "b" is also marked as final .