2017-11-12 8 views
1

나는이 생성자가 :내가 가진 생성자로 매개 변수를 owerwrite하지 않으려면 어떻게해야합니까?

private static int list [] = new int[0]; 
    public IntList (String[] elems){ 
     this.list = new int[elems.length]; 
     int j=0; 
     for(String i : elems){ 
      this.list[j] = Integer.parseInt(i); 
      ++j; 
     } 
    } 

을 그리고 나는 새로운 IntList를 정의하면 나는 원래 하나의 args을 볼 수있는 것보다.

public static void myTest(IntList args){ 
    String[] tmpIntList = {"21","22","23","24"}; 
    IntList newIntListForTest = new IntList(tmpIntList); 
    /* for example, if I called myTest with {"1","2","3"}, 
     and if I print args here then I see only 21,22,23,24*/ 
} 

둘 모두를 보려면 어떻게해야합니까?

답변

4

사용자가 list 인 경우 static이며 특정 인스턴스가 아닌 클래스에 속한 것입니다. 즉, IntList의 모든 인스턴스는 동일한 list을 공유하므로 새 인스턴스를 만들고 list을 덮어 쓸 때마다 "IntList 모든 인스턴스에 대해"재정의됩니다.

private int[] list = new int[0]; 
다음 static 수정 제거, 당신은 괜찮을 것 -

는 긴 이야기를 짧게 만들려면