我一直在尝试像教程所说的那样在 Java 中声明一个数组,但收到了一个错误。这是我的代码:
public class ArrayExample {
private static final int SIZE = 15;
/* this works */
int[] arrayOfInt = new int[SIZE];
/* but this doesn't work, says "cannot find symbol" */
int[] arrOfInt;
arrOfInt = new int[SIZE];
public static void main(String[] args) {
/* but here it works; why? what's the difference? */
int[] arrOfInt;
arrOfInt = new int[SIZE];
}
}
我在教程中找不到这种差异的解释。为什么第二个声明不起作用,但main
方法中的第三个声明起作用?