0

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;
}
Braj
  • 46,415
  • 5
  • 60
  • 76
  • For your last case, it works because flag has not been assigned. If you change the line the first line to `final boolean flag = false`, it will no longer work – Chip Aug 24 '14 at 11:22
  • @Chip yes I know it, Can't assign value again for `final` variable. – Braj Aug 24 '14 at 11:26
  • So finally the conclusion is: *We can't assign final variable inside the loop*. Thanks to all for your valuable time. – Braj Aug 24 '14 at 11:32
  • That's not entirely true - you can if you have not assigned it before. – Chip Aug 24 '14 at 11:35
  • @user3218114-As i mentioned, that the initialisation of final variables take only once, so after adding `break`,compiler knows that this loop will run only once. So,it allows the same. I hope you got what you wanted! – Am_I_Helpful Aug 24 '14 at 11:36
  • 1
    @Chip we can if we add a `break` statement. – Braj Aug 24 '14 at 11:36
  • 1
    Yes I got it when I said Thanks to all. – Braj Aug 24 '14 at 11:36
  • Nice question though! – Am_I_Helpful Aug 24 '14 at 11:37
  • @user3218114 you don't need `break`. You just change the first line : `final boolean flag;` instead of `final boolean flag = false;` – Chip Aug 24 '14 at 11:37
  • @Chip I know about it. We can't assign value again for `final` variable once assigned. That's a different concept. – Braj Aug 24 '14 at 11:39

3 Answers3

4

Case 1: 'flag' is being assigned each time you iterate in 'while' loop. You are using assingment operator '=' instead '=='.

Case 2: 'flag' always is false, so code inside 'if' will never execute.

Ezequiel
  • 3,477
  • 1
  • 20
  • 28
  • compiler knows about the value of `final` variable at compilation time then why it doesn't talk about unreachable code. – Braj Aug 24 '14 at 11:08
  • becaus 'flag = false' is a assingment, doen't return a boolean vaule, and 'if' statment needs a boolean value. – Ezequiel Aug 24 '14 at 11:11
  • 2
    assignment statements return the assigned value in java – Braj Aug 24 '14 at 11:12
  • @user3218114 you can assign a value to a final variable only once. That's why your assignment in the while loop fails – Chip Aug 24 '14 at 11:15
  • You have the reason +1 – Ezequiel Aug 24 '14 at 11:15
  • In first case, As per my understanding compiler knows that `final` variable is initialized with `false` hence it should says unreachable code. – Braj Aug 24 '14 at 11:15
  • The compiler warns you about one violation. Its saying you are trying to assign a final variable multiple times (loop). – Ezequiel Aug 24 '14 at 11:19
  • @user3218114 Logically, in the first case, to understand that the code is unreachable it has to first understand what flag=false means. While "understanding" this, it tells you immediately that flag = false is invalid because it is final and has been assigned once. That's why you get that error instead of unreachable code. – Chip Aug 24 '14 at 11:21
  • It means the compiler is not so smart that it can check the value of `final` variable at the time of compilation. – Braj Aug 24 '14 at 11:21
  • 2
    A compiler works in a number of phases. The phase to check unreachable code comes later than the assignment check. Generally in compilers, once a error has been detected, it does not continue the next phases. This, in general, reduces the number of errors and helps you solve your compilation error faster. – Chip Aug 24 '14 at 11:24
1

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;

Air
  • 8,274
  • 2
  • 53
  • 88
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • *the first one is showing error related to flag variable incorrect declaration* what does it mean? where it is incorrectly declared? – Braj Aug 24 '14 at 11:14
  • Check my last paragraph taken from Wikipedia! – Am_I_Helpful Aug 24 '14 at 11:14
  • you are taking about `instance` blank `final` variable. I am talking about `local final` variable. – Braj Aug 24 '14 at 11:16
  • No,@user3218114- Your 1st loop is also an infinite loop,in this case `flag` being a final variable is always getting initialised at iteration of each loop---thereby throwing compilation error as to how this is possible! – Am_I_Helpful Aug 24 '14 at 11:18
  • why it's an infinite loop? I have initialized it to `false` The code inside the loop will never be reached. – Braj Aug 24 '14 at 11:19
  • Oh,SORRY! But,to assume that it runs as compiler don't know at compile-time whether it is gonna run how many times. It is done only at run-time! – Am_I_Helpful Aug 24 '14 at 11:21
  • but compiler knows in second case. – Braj Aug 24 '14 at 11:24
  • That's what I mentioned,compiler knows in second case---so giving `flag=false` initialisation only at the beginning of declaration! Not inside the body of the loop! Hence,it gives that code unreachable as it won't reach inside the loop ever because final variable declared earlier at declaration.I hope you got it!If not,feel free to extend the comments! – Am_I_Helpful Aug 24 '14 at 11:28
  • So finally the conclusion is: `We can't assign final variable inside the loop`. – Braj Aug 24 '14 at 11:31
  • just delete the above comment. – Braj Aug 26 '14 at 10:50
1

From the comments, it seems that you are looking for why the first code does gives you a The final local variable flag may already have been assigned instead of Unreachable error.

This is because there are various stages of java compilation as detailed here : http://openjdk.java.net/groups/compiler/doc/compilation-overview/

The expressions are evaluated in the first stage (Attr ). Flow Analysis is done only if there is no errors in the first stage. Since the illegal assignment was caught in the first stage, the second stage (Flow) will not get executed, and you will not get Unreachable Error

Chip
  • 3,226
  • 23
  • 31
  • 1
    the answer is that `final` variable should be initialized just once. In case of `while` (without `break` statement) the compiler says *The final local variable flag may already have been assigned* because it may come back to condition in next iteration. – Braj Aug 24 '14 at 11:42