Compiler error when declaring a variable inside if condition and no curly braces
问题 Why does this first if compile well and the second fail? if(proceed) {int i;} // This compiles fine. if(proceed) int i;// This gives an error. (Syntax error on token \")\", { expected after this token) 回答1: Because the language spec says so: http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html A declaration introduces an entity into a program and includes an identifier (§3.8) that can be used in a name to refer to this entity. A declared entity is one of the following: ... A local