Reputation: 506
im trying to use the short-circuit evaluation to simplify the writing of a check , but eclipse calls "The local variable inherit may not have been initialized" in the last statment of the if clause, am i using the evaluation method correctly ? Can the IDE understand the evaluation of the statement ?
if ((className.startsWith("Svl")) &&
((inherit = aAST.findFirstToken(TokenTypes.EXTENDS_CLAUSE)) == null)
|| !(inherit.getText().equals("Servlet******"))) {
log(aAST.getLineNo(), "error" + tokenIdent.getText());
}
Upvotes: 0
Views: 162
Reputation: 200226
You have got your parentheses wrong, that's what is causing the error. Simplified, the structure of your boolean expression is
(A && B) || C
where in B you assign to inherit
. if A fails, (A && B) is short-circuited to false, B is not evaluated, and C needs to be evaluated to find out the final result. Clearly, inherit
may not have been initialized when C is evaluated.
Depending on what you are actually after, you can swap the positions of A and B. That will ensure B is evaluated always. Alternatively, what you really need may be A && (B || C). The requirements are up to you.
Upvotes: 7