I'm writing a simple program for a task I'm working with. However, I get the error "Cannot make a static reference to the non-static field employees" when I try to move the ArrayList "employees" outside the main method.
Code that works:
public class X{
public static void main(String[] args){
ArrayList<Employee> employees = new ArrayList<Employee>();
while(i<6){
int skill = generator.nextInt(5);
int id = generator.nextInt(100); //for this purpose we will never
Employee newFresher = new Employee(id, skill);
employees.add(newFresher);
System.out.println(newFresher);
i++;
}
}
public void getCallHandler(){
//CODE THAT REALLY NEEDS TO SEE THAT ARRAYLIST
}
}
Code that throws error "Cannot make a static reference":
public class X{
ArrayList<Employee> employees = new ArrayList<Employee>();
public static void main(String[] args){
while(i<6){
int skill = generator.nextInt(5);
int id = generator.nextInt(100); //for this purpose we will never
Employee newFresher = new Employee(id, skill);
employees.add(newFresher);
System.out.println(newFresher);
i++;
}
}
public void getCallHandler(){
//CODE THAT REALLY NEEDS TO SEE THAT ARRAYLIST
}
}
I just have no idea what's causing this. Help would be greatly appreciated.
PS: Ignore the weird indentation. It's just stackedoverflow formatting it weird.