简体   繁体   中英

Eclipse and short circuit evaluation

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());
        }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM