I don’t spot any mistake on this code, however eclipse tells me that the variables are not initialized. It is only a warning, but after compiling it doesn’t work either. I simply can’t find the mistake and thing of this code being 100% correct. Please note that the structure of this code can not easily be changed because the code provided below is simplified so you do not have that much to read 😉
int min1; float somefloat;
try {
//setting values for min and somefloat
min1 = 1;
somefloat = 0.92f;
} catch (IOException ioe) {
System.err.println("Read Exception");
}
while (true){
//warning: variables min1 and somefloat may not be initialized.
float value1 = (1023 - min1) * somefloat;
System.out.println("Value: " + value1);
}
Compiler doesn’t analyse if in concrete case the variables would be initialized or no. It assures that if the variables are initialized only in try, and not in catch or finally it would assure they may be not initialized.
Give a similar try with “obvious” condition like
if(1>0) {}. The compiler compiles and not makes the analysis of your code. While for human it is obvious that something will happen, the java compiler have no code to detect such cases, neither it is specified by java syntax. So you’re expecting from the compiler the AI it doesn’t have, to keep the compiling clear, predictable and fast.Your code has, BTW, error that will be reported instead of this you describe, because there’s no place
IOExceptioncan be thrown.