Search code examples
javaif-statementwhile-loopfinal

Different compilation error when final local variable is used with while loop


Sample code:

Compilation error:

The final local variable flag may already have been assigned

final boolean flag;
while (flag = false) { // I am using = instead of == just to test it
    System.out.println("inside loop");
}

Compilation error:

Unreachable code

final boolean flag = false;
while (flag) {
    System.out.println("inside loop");
}

I know:

  • The local variable must be initialized before its first use.
  • As per the coding standard the final local variable must be initialized at the time of declaration.

Questions:

  • What is the difference between these statements? As per my understanding both are same.
  • Why first sample code doesn't talk about unreachable code. The second compilation error is clear to me.

If works fine with if condition

final boolean flag;
if (flag = false) { // no compilation error
    System.out.println("inside if block");
}

If works fine if I add a break statement in the while loop that ensures the compiler that the final local variable can be initialize just one in its life.

final boolean flag;
while (flag = false) {
    System.out.println("inside if block");
    break;
}

Solution

  • What is the difference between these statements? As per my understanding both are same.

    How can both your codes be same??? In your first case, you are initialising your flag variable with false, so it will be assuming initailisation of flag variable with each iteration of loop (decision taken by compiler at compile-time as it doesn't know about the no. of iterations), BUT IN REALITY THIS CODE WON'T BE EXECUTED EVER---thereby treating it as a non-final variable, hence, contradiction, whereas in second case it also won't run as the flag has already been declared false at the declaration step only, NOT INSIDE THE BODY OF LOOP!!! So, the second one will be an infinite loop!

    final boolean flag;
    while (flag = false) {                   // First code---flag final variable initialised with each iteration
    System.out.println("inside loop");
    }
    
    final boolean flag = false;
    while (flag) {                          //  Second code
    System.out.println("inside loop");
    }
    

    Why first sample code doesn't talk about unreachable code. The second compilation error is clear to me.

    Because either of the compilation error is reported first. In this case, the first one is showing error related to flag variable incorrect declaration. As taken from Wikipedia:

    A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a "blank final" variable. A blank final instance variable of a class must be definitely assigned in every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared;