In the following class, I have a non-static ArrayList
. I am making a non-static method call (add()
) on that ArrayList
. But my IDE is telling me that I am making a static call (Cannot make a static reference to the non-static field arrayList
).
import java.util.ArrayList;
public class Test {
private ArrayList<String> arrayList = new ArrayList<String>();
public static void main(String[] args) {
arrayList.add("str");
}
}
Why is this not allowed? If I declare the ArrayList within the static method (main), it works. But I am not understanding why the method affects the ability to call a non-static method on a non-static variable.
EDIT: I know how to solve the problem...my question is, why is this happening in the first place?