Java: Local variable is initialized, still get an error

不羁岁月 提交于 2019-12-11 04:09:16

问题


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);
}

回答1:


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 IOException can be thrown.




回答2:


You have to initialize all your variables before they enter any try block. The Java compiler does not know that in your case, there is no way of an exception to be caused.

So theoretically speaking, there could be an exception somewhere which leaves your variables uninitialized. And that's not acceptable. Thus the warning.

Just initialize your variables to zero.

int min1 = 0;
float somefloat = 0f;



回答3:


however eclipse tells me that the variables are not initialized

The warning is shown cause it is a possiblity that exception is thrown before the variable are initialized within try. then within while the variables remain uninitialized.




回答4:


They should have an initial value just in case if it enters the catch block. Try this:

int min1 = 0; float somefloat = 0f;
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);

}




回答5:


You have to initialize them while declaring them or at least outside the try-catch block. Because compiler cannot be sure the initialization inside the try-catch block is going to complete normally, in which case, you will be having an un-initialized variables outside.

int min1=0; float somefloat=0.0f;



回答6:


you need to initialize the variables during declaration.variable assignment in try will overwrite that value and if any exception occur(Don't know how in your case..) it will take the default value. during compilation, compiler will not look at your code whether it will throw an exception or not (except throw , it requires a matching catch) so it needs an explicit initialization .




回答7:


private void tset(){
    int min1; float somefloat;
    try {
        //setting values for min and somefloat
        min1 = 1;
        somefloat = 0.92f;
    while (true){
        //warning: variables min1 and somefloat may not be initialized.
        float value1 = (1023 - min1) * somefloat;
        System.out.println("Value: " + value1);
    }
    } catch (Exception ioe) {
        System.err.println("Read Exception");
    } 
}

This may be the code you are looking for...



来源:https://stackoverflow.com/questions/14831974/java-local-variable-is-initialized-still-get-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!