public class Test {

public int N;

public Test(int n){

N = n;

}

public int[] arr = new int[N];

public static void main(String[] args){

Test wow = new Test(100);

wow.arr[0]=3;

}

}


이러면 wow.arr 배열 메모리가 안생기는 거 같은데 왜 그런지 이해가 안가.  java.lang.ArrayIndexOutOfBoundsException 에러 뜨거든..

여러번 삽질해보니까 아래처럼 생성자 메서드에서 arr도 초기화시키면 문제는 해결되는데 처음꺼 처럼하면 왜 에러가 뜨는지 모르게씀..

public class Test {

public int N;

 public int[] arr;

public Test(int n){

N = n;

   arr = new int[N];

}

public int[] arr = new int[N];

public static void main(String[] args){

Test wow = new Test(100);

wow.arr[0]=3;

}

}