public class SomeClass{
public static void main (String[] args){
if(true) int a = 0;// this is being considered as an error
if(true){
int b =0;
}//this works pretty fine
}
}//end class
在上述第一类if语句表示编译错误错误上如果条件的java
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "int", delete this token
a cannot be resolved to a variable
然而第二if语句工作正常。我自己弄不明白。我知道在单个语句中声明一个变量是没有用的if
。这两种说法如何不同,请给我解释一下。如果问题很简单,不好意思。
在第二个例子中,你可以加入一些代码使用'B',而这是很难设计一个可以使用'a'的例子。 –
[为什么控制结构的条件需要在一个块中声明](http:// stackoverflow。com/questions/13647323/why-do-declarations-following-conditions-of-a-b) – jlordo