t*****z 发帖数: 812 | 1 为什么第一个fail,第二个通过?不就调了一下循序么,百思不解
**fail
public static void main ( String args[] ) {
int x = 50;
System.out.println("x= "+x);
{
int x= 30; //compilation fail: x is already defined in main
System.out.println("x= " + x);
}
}
**pass
public static void main ( String args[] ) {
{
int x= 30;
System.out.println("x= " + x);
}
int x = 50;
System.out.println("x= "+x);
} | m*****u 发帖数: 1342 | 2 I suspect in the second case, the x defined earlier was out of scope when
the second x is being declared, which is not true for the first case.
【在 t*****z 的大作中提到】 : 为什么第一个fail,第二个通过?不就调了一下循序么,百思不解 : **fail : public static void main ( String args[] ) { : int x = 50; : System.out.println("x= "+x); : { : int x= 30; //compilation fail: x is already defined in main : System.out.println("x= " + x); : } : }
| r*****l 发帖数: 2859 | 3 Your understanding is right.
【在 m*****u 的大作中提到】 : I suspect in the second case, the x defined earlier was out of scope when : the second x is being declared, which is not true for the first case.
|
|