java变量的作用域
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class OneClass { static int x;//会赋值默认值0 public static void main(String[] args){ //int x = 2; //不能在同一个方法内定义同一个变量,不然会报错 Duplicate local variable x System.out.println("第一个x:"+x); { int x = 5;//在方法内的变量必须初始化,否则x会是随机值而不是0 //如果不赋值会报错The local variable x may not have been initialized System.out.println("第二个x:"+x); } } } |
继续关注