1

i have:

  if (Constants.IS_LIVE_MOD == false)
        account = Constants.OM_ACCOUNT;
  else
        account = "abc";

i am getting a dead code warning on 'else'. Why is it so and wat is the solution for it. Please help.

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
  • We need to see more code. What's the value of Constants.OM_ACCOUNT? – ssedano Aug 11 '11 at 10:24
  • 3
    Can Constants.IS_LIVE_MOD ever be true? The fact that it's a constant suggests not, in which case the 'else' will never be executed. – James Aug 11 '11 at 10:25

12 Answers12

10

I assume the IS_LIVE_MOD constant is a final variable that is declared as false, if so then the variable is always false and can't be changed, and so the else statement will never be invoked. Therefore it is dead code.

E.g.

private static final boolean MY_VAR = false;

if(MY_VAR == false) {
    System.out.println("Always does this");
}
else {
    System.out.println("Dead code");
}
stephendnicholas
  • 1,810
  • 14
  • 15
  • 2
    No, don't remove `final`. Just ignore the warning. There is no better way to do `#ifdef` in Java... – Thilo Aug 11 '11 at 10:28
  • 2
    Is the constant ever going to change? If not, then you don't really need the if statement. If you just want the warning to go away you can use the suppresswarnings annotation to tell the compiler to ignore it: http://knol.google.com/k/suppresswarnings-annotation-in-java – stephendnicholas Aug 11 '11 at 10:31
  • just remove the comparison, see my answer below – user85421 Sep 05 '12 at 13:29
5

If IS_LIVE_MOD is a compile-time constant with the value false, the compiler knows that the "else" path will never be taken, so gives you a warning. (The Java language specification guarantees that it won't be an error, but it's reasonable to warn you.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

if Constants.IS_LIVE_MOD is a constant and its value is false than the compiler knows that else would never be executed.

You can't have it as a constant.

Ankur
  • 12,676
  • 7
  • 37
  • 67
3

You're are comparing two constants (Constants.IS_LIVE_MOD and false) with each other. As they are constant, the result can be determined at compile time. So the compiler can tell which part will never be executed.

Codo
  • 75,595
  • 17
  • 168
  • 206
2

The condition if (Constants.IS_LIVE_MOD == false) always evaluates as true. Therefore else is never reached.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
M Platvoet
  • 1,679
  • 1
  • 10
  • 14
2

It implies that Constants.IS_LIVE_MOD is always equal to false; so the else clause is never executed; hence the dead code warning.

Brett Walker
  • 3,566
  • 1
  • 18
  • 36
2

The meaning of the warning should be clear: the code will not be executed - is dead - since IS_LIVE_MOD is a constant, but here is one solution (workaround):

if (!Constants.IS_LIVE_MOD)
    account = Constants.OM_ACCOUNT;
else
    account = "abc";

or

if (Constants.IS_LIVE_MOD)
    account = "abc";
else
    account = Constants.OM_ACCOUNT;

Details JLS 14.21: Unreachable Statements

If you remove the comparison, the compiler recognizes that as a "conditional compilation" and does not show that warning.

Rationale (JLS):

in order to allow the if statement to be used conveniently for "conditional compilation" purposes

user85421
  • 28,957
  • 10
  • 64
  • 87
1

If Constants.IS_LIVE_MOD is constant (as its name implies) and is false, then the else clause can never possibly run; this makes it dead code.

mah
  • 39,056
  • 9
  • 76
  • 93
1

Since IS_LIVE_MOD is a constant, the compiler figures out that it will also be false, and that the else part will never be used (until you change your Constants, then it will be the other way around).

Don't worry about it. Ignore or suppress the warning (@SuppressWarnings("all") on the method, unfortunately has to be all, I think).

If you have "real" dead code, it will make an error.

Thilo
  • 257,207
  • 101
  • 511
  • 656
1

How is Constants.IS_LIVE_MOD declared? If it's a final field, it's a good chance that the compiler optimizes this expression to be always true.

RoToRa
  • 37,635
  • 12
  • 69
  • 105
1

I'd say the most efficient way of writing this would be like this:

account = Constants.IS_LIVE_MOD ? "abc"
                                : Constants.OM_ACCOUNT;
Custard
  • 766
  • 1
  • 7
  • 15
  • Without wanting to appear rude, that doesn't really help much with the problem at hand. Also, I'm always a little wary of using the ternary operator because I personally find it makes the code less readable and there's little or no performance increase. See here for some (rather dated) discussion: http://www.coderanch.com/t/391402/java/java/Ternary-operator-if-condn – stephendnicholas Aug 11 '11 at 10:39
  • Yes, you are correct. It doesn't answer the question. I only joined today, so I'll put it down to being a noob. – Custard Aug 11 '11 at 10:50
1

I guess Constants.IS_LIVE_MOD is a compile time constant with the value set to false. iow it becomes

if (false == false)
    account = Constants.OM_ACCOUNT;
else
    account = "abc";

You can just ignore the warning since Java doesn't seem to support conditional compilation Java conditional compilation: how to prevent code chunks from being compiled?

Community
  • 1
  • 1
Dirk
  • 1,184
  • 6
  • 22