Please read the post before marking it as duplicate.
and I am not a Java expert
This is a simplified code of what I am trying to do:
import java.util.ArrayList;
public class Parameter {
private static ArrayList<Integer> quantityArray = new ArrayList<>();
public static void quantityPriorityValues() {
int q = 0 ;
for (int i = 0; i < 5 ; i++ ) {
quantityArray.add(q);
q++;
}
}
public static ArrayList<Integer> getQuantityArray() {
return quantityArray;
}
public void setQuantityArray(ArrayList<Integer> quantityArray) {
this.quantityArray = quantityArray;
}
}
public class Connection {
public void getParameters () {
i = 0;
Parameter.quantityPriorityValues();
while (i < 5) {
int quantity = Parameter.getQuantityArray().get(i);
System.out.println(quantity);
i++;
}
}
}
It always prints out the last value of the arraylist.
I checked answers about this problem , like this one and this one and they both said that the problem in in the static
fields.So I tried to remove the static
from ArrayList<Integer> quantityArray
but that means that the getters and setters should be non-static
too and I won't be able to call the getter in the Connection
class.
I want this output: 0 1 2 3 4
But I get this output: 4
Does anyone have another solution?