Please consider the follwing code
code:
public class Fiz
{
int x = 5;
public static void main(
String[] args)
{int x;
final Fiz f1 = new Fiz();
Fiz f2 = new Fiz();
Fiz f3 = Fizswitch(f1,f2);
if(false){ //line 1
f1=f2;//line 2
System.out.print("sdfdsf");
}
System.out.println(f1.x = f3.x);
}
static Fiz Fizswitch(Fiz x, Fiz y){
final Fiz z =x;
z.x =6;
return z;
}
}
at line 2 i get a compiler error: cannot assign a value to final variable f1
f1=f2;
^
1 error
why cant the compiler tell that the if block will not run even with a literal value of false in the if
test condition? where as in the below code it can
code:
public class TestLocal {
public static void main(String [] args) {
int x;
boolean b = true;
boolean c = b;
if (false) { // always false
x = 7; //
}
int y = x;//line 1
System.out.print(x);
}
}
at line1 i get compiler error : variable x might not have been initialized
int y = x;
^
1 error
In the first case compiler still able to get inside the if(false) condition but in the second case compiler not able to get inside the if(fasle) condition...can somebody explain please? thanks