Java variable scope in if statement [duplicate]
问题 This question already has answers here : Declaring a useless local variable (2 answers) A single-line loop with a mandatory pair of braces in Java (3 answers) Closed 4 years ago . I received a compilation error for the following code: if(true) int a = 10; else int b = 20; If I change it to the following code, then there is no compilation error: if(true) { int a = 10; } else { int b = 20; } Why is the first syntax wrong, and from what language standard? 回答1: The Java specification says that an