-3

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.

user2864740
  • 60,010
  • 15
  • 145
  • 220
user1531921
  • 1,372
  • 4
  • 18
  • 36
  • [Search first](http://stackoverflow.com/search?q=%22Cannot+make+a+static+reference+to+the+non-static+field%22). This code is not unique. – user2864740 Jun 12 '14 at 18:08
  • 2
    The formatting problem is due to you using tabs. Convert your code to use spaces for indentation, and it will be fine. – Jon Skeet Jun 12 '14 at 18:09
  • possible duplicate of [Why is there a problem with a non-static variable being read from main?](http://stackoverflow.com/questions/4665089/why-is-there-a-problem-with-a-non-static-variable-being-read-from-main) (and also see http://stackoverflow.com/questions/2042813/calling-non-static-method-in-static-method-in-java?lq=1) – user2864740 Jun 12 '14 at 18:12

2 Answers2

1

main() method is a static method and you can only access static fields from in there. So you have 2 choices here:

  1. just mark the employees field as static and then you can access it directly from your main method.
  2. Or, if you don't make that field static, then it remains an instance field. In that case, move all your logic of main method in an instance method (processEmployees()), and create an instance of your class ( X x = new X(); ) and then call this method (x.processEmployees()).
V_Singh
  • 729
  • 11
  • 22
0

The problem is, the employees list is not static, yet you are using it within a static method. You can't do this, static means one for every instance of this class, whereas not static would mean one for each instance. Either make employees static, or don't use it within a static method, do something like new X() in the static method and the usage of employees in the constructor:

public X() {
   // use employees here!
}
Nathan
  • 1,135
  • 2
  • 12
  • 27
Isaac Woods
  • 1,114
  • 1
  • 17
  • 28