私はチュートリアルのようにJavaで配列を宣言しようとしていますが、間違いを受けました。ここに私のコードです:mainクラスまたはmainメソッドで配列を宣言することの違いは何ですか?
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];
}
}
私はチュートリアルでこの違いの説明を見つけることができませんでした。なぜ2番目の宣言は機能しませんが、main
メソッドの3番目の方法は動作しますか?
main()内で定義されたものはmain関数内のローカル変数に過ぎず、他の場所では使用できません。 main()関数の上で定義されたものは、クラスのメンバーになり、すべてのメソッドで使用できます。 –