Declaring a useless local variable

对着背影说爱祢 提交于 2019-11-29 09:17:58

If you have a if, for, while, do/while you must follow it with a statement. A declaration is not a statement.

From JLS 14.9 - The if Statement

IfThenStatement:
    if ( Expression ) Statement

IfThenElseStatement:
    if ( Expression ) StatementNoShortIf else Statement

IfThenElseStatementNoShortIf:
    if ( Expression ) StatementNoShortIf else StatementNoShortIf

I assume they do this because any variable you declare couldn't be used as it would be out of scope immediately (except the same declaration)

My guess is that a declaration isn't an executable statement and a declaration with an assignment is really broken into two different statements by the compiler, with the first statement (the declare) not being executable. An if requires an executable construct, either a block or an executable statement.

I know that I've not been able to put a breakpoint on a non-sassignment declaration statement in the Eclipse debugger. Probably the same underlying reason.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!