0

So I recently came accros this. I was creating an if-else-statement with as it's condition a final boolean variable. Eclipse immediatly told me that the else part of the code was unreachable and therefore dead code. This was my code(compacted).

public static void main(String[] args){
    final boolean state = true;
    if(state == true){
        System.out.println("A");
    }else{
        System.out.println("B");
    }
}

Then I though what would happen if the code stayed the same but the variable wasn't final anymore? So I tried that and this was what happened, nothing no warnings or errors. The code:

public static void main(String[] args){
    boolean state = true;
    if(state == true){
        System.out.println("A");
    }else{
        System.out.println("B");
    }
}

Now I'm wondering, why is the first case detected and flagged and the second case not?

Thank you in advance.

Roan
  • 1,200
  • 2
  • 19
  • 32

4 Answers4

2

Try this as an alternative.

public class Test061 {
    public static void main(String[] args){
        int a = 0;
        if(1 == (a+1)){
            System.out.println("A");
        }else{
            System.out.println("B");
        }
    }
}

There are still no warnings. Why?

Because the compiler does not execute your code. It only can issue a warning when it sees some "constants" or "constant expressions". Apparently when you put final the compiler then knows that this value cannot change. While if you use "real variables" or "variable expressions", it doesn't know because it doesn't execute your code (to see what the value of state or (a+1) is at this line). So in the latter case, you get no warnings. Hope this makes more sense now.

Think of it this way: the compiler does some code analysis. The first basic pattern is detected by this analysis, while the second pattern is not (probably as it's not that basic).

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • I does make sense but I still excpected that the compiler would check if the value is changed before the statement. – Roan Jun 14 '14 at 13:06
  • 1
    Yeah, sure, I get your point, it makes perfect sense too. I also expected many different things from many languages and frameworks. But the reality is often different from the expectations. – peter.petrov Jun 14 '14 at 13:08
  • Yeah probably there are always unexpected things to happen. – Roan Jun 14 '14 at 13:10
  • 1
    'would check if the value is changed before the statement.' - THAT would be great! Seeing Nullpointer Exceptions during DesignTime :) – dognose Jun 14 '14 at 13:15
  • Oh yeah that would for sure be amazing :). – Roan Jun 14 '14 at 13:16
1

final means that something cannot be changed, so likely it was flagged because it can never and will never reach that else statement.

On a side note, you never have to do if(Boolean == true) you can just do if(Boolean)

jhobbie
  • 1,016
  • 9
  • 18
  • Usually you only attach `final` to constants – jhobbie Jun 14 '14 at 12:56
  • Yeah I knew that about the final thing but I expected that the compiler would detect the other thing aswell. And the sidenote I'll try to remember. Thanks for the answer. – Roan Jun 14 '14 at 12:59
1

The compiler does some optimization for final variable as shown below and in that case compiler knows that else part will never reached because final variable can't be changed later.

if (true) {
    System.out.println("A");
} else {
    System.out.println("B");
}

Read more JLS §4.12.4. final Variables

Find more possibilities for Unreachable code compiler error

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
1

First example:

When we use final keyword with a variable and assign a value to the variable, then that value can't change again. final boolean state = true; It can't have a value of "false".

Second example:

Here the variable state is not final. It has the possibility to get a value of "false".

So the different behavior is because of the final keyword.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • That true but in this case the state variable can't be changed after the if-statement either. – Roan Jun 14 '14 at 13:02
  • @Roan Right. But this the compiler know this? Does it execute your code really, or it just looks for some simple rules/patterns to detect some obvious issues and to classify them as warnings? – peter.petrov Jun 14 '14 at 13:04
  • Yeah but I expected one of those rules to be check if a variable changes before an statement. – Roan Jun 14 '14 at 13:09