Reputation: 9020
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.
Upvotes: 1
Views: 8198
Reputation: 29680
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
Upvotes: 2
Reputation: 1654
The condition if (Constants.IS_LIVE_MOD == false)
always evaluates as true
. Therefore else
is never reached.
Upvotes: 2
Reputation: 1830
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");
}
Upvotes: 10
Reputation: 1234
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?
Upvotes: 1
Reputation: 262474
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.
Upvotes: 1
Reputation: 786
I'd say the most efficient way of writing this would be like this:
account = Constants.IS_LIVE_MOD ? "abc"
: Constants.OM_ACCOUNT;
Upvotes: 1
Reputation: 12774
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.
Upvotes: 5
Reputation: 38390
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.
Upvotes: 1
Reputation: 1499950
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.)
Upvotes: 5
Reputation: 78815
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.
Upvotes: 3
Reputation: 39807
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.
Upvotes: 1
Reputation: 3576
It implies that Constants.IS_LIVE_MOD is always equal to false; so the else clause is never executed; hence the dead code warning.
Upvotes: 2