0

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?

Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • Emphasis: `static` method, non-`static` variable. – Sotirios Delimanolis Oct 31 '14 at 02:09
  • Can you elaborate please? I am aware. I don't see why the method I am in matters. The variable is not static, and the method I am calling on the variable is not static. – Evorlor Oct 31 '14 at 02:09
  • 2
    Non static variables belong to instances. You don't have an instance. – Sotirios Delimanolis Oct 31 '14 at 02:10
  • Your method is static. You can only use static variables in static method. If you make arrayList static, it will remove the error. – Carlos Bribiescas Oct 31 '14 at 02:11
  • EDIT: I know how to solve the problem...my question is, why is this happening in the first place? – Evorlor Oct 31 '14 at 02:13
  • @SotiriosDelimanolis Another (shortcut) way to think of it is that it belongs to the static instance. Of which there is only one. That is how I originally learned it. – Carlos Bribiescas Oct 31 '14 at 02:13
  • 2
    The key to answering the question is understanding the one-per-instance vs. one-per-class distinction. When you declare a static variable, it becomes the only such variable in the entire system. When you declare an instance variable, it becomes one of potentially many such variables, each one attached to its own instance. A static variable can be identified by its name: it is sufficient, because there's only one. For instance variables, however, a name alone is no longer sufficient, because you need to know two things - what is the name of the variable, and to which instance it is attached. – Sergey Kalinichenko Oct 31 '14 at 02:26
  • @dasblinkenlight green checkmark. Thanks! (Along side the infamous Jon Skeet's answer http://stackoverflow.com/questions/6615909/c-sharp-accesing-non-static-member-in-a-static-function) – Evorlor Oct 31 '14 at 02:30

1 Answers1

0

It should be:

private static ArrayList

Not making the variable static and using it in a static method is not acceptable because a non static variable in a class requires the class/variable to be initialized.

You could create a separate class with a constructor and instantiate the object if you want to use it that way... for whatever reason.

Alternatively, you could initialize your class in your static method after adding a constructor.

More info/duplicate question:

What does the 'static' keyword do in a class?

Community
  • 1
  • 1
user1274820
  • 7,786
  • 3
  • 37
  • 74