2

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

7 Answers 7

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;
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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.

Comments

0

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

}

Comments

0

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;

7 Comments

That's the question. They are initialized within the try block before any exception can occur.
but in a try-catch block there normally can be an error. if there is an error, the intialization might be skipped - and then they are not initialized.
@jlordo well, local variables need to be initialized before you use them.
@jlordo. Point is not that they are initialized before any exception can occur. Compiler can't be sure that you won't add any such code at the later stage before that initialization.
@PremGenError. Added some more stuffs to make your answer more sensible.
|
0

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 .

Comments

-1
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...

1 Comment

Using internet shorthand ("b", "u", "r") is frowned upon here. Please try to write complete sentences with real words. It makes reading and understanding a lot easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.