Korte Alma
Korte Alma

Reputation: 168

How to not owerwrite the parameter with constructor which I got?

I have this contructor:

    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;
        }
    }

And if I define a new IntList than I can't see the original one, the 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*/
}

How can I do it to see both of them?

Upvotes: 1

Views: 45

Answers (1)

Mureinik
Mureinik

Reputation: 311073

You list member is static, meaning it belongs to the class, not a specific instance. In other words, all the instances of IntList share the same list, so whenever you create a new instance and overwrite list, it's overriden "for all the IntLists".

To make a long story short - remove the static modified, and you should be fine:

private int[] list = new int[0];

Upvotes: 4

Related Questions