简体   繁体   中英

Java ArrayList of objects' add() method not working

Im new to JAVA, and Im facing a beginer's problem, I know :-P

IN PLAIN ENGLISH => I'm trying to create an ArrayList of objects, and to add new objects in the array when required.

OK, so here is a compact version of my code

package ACP.Employee;   //created package
import java.util.ArrayList;     //imported arraylist class
import ACP.Employee.EmployeeClass;  //imported employee class of same package

public class ClientClass
{
    ArrayList<EmployeeClass> empArray = new ArrayList<EmployeeClass>(); //created a new Array List

    public static void main (String[] args)
    {
        int objcount = 0;   //variable to store objct count

        empArray.add(objcount, EmployeeClass obj);
    }
}

The IDE (Eclipse) gives me following errors on empArray.add line

  • Cannot make a static reference to the non-static empArray
  • EmployeeClass cannot be resolved to a variable
  • Syntax error on token obj, delete this token.

I have also tried to replace that line with the following syntax,

empArray.add(new EmployeeClass());

BUT the following error remains.

  • Cannot make a static reference to the non-static empArray

Kindly help out here, I have seen API Spec of ArrayList's add() method, which is following:::

void add(int index, Object element) ==>> Inserts the specified element at the specified position index in the list boolean add(Object o) ==>> Appends the specified element to the end of this list. SOURCE ( http://www.tutorialspoint.com/java/java_arraylist_class.htm )

There are two problems with your code.

  1. You are trying to access a member of the class ClientClass from a static context (main). This is only possible if that member is static. Since empArray has no modifiers it will default to package-private which is not static. You will either have to make it accessible in a static context by declaring it static:

    static ArrayList<EmployeeClass> empArray = new ArrayList<>();

    or create an instance of ClientClass and access its member

     ArrayList<EmployeeClass> empArray = new ArrayList<EmployeeClass>(); public static void main(String[] args) { ClientClass t = new ClientClass(); t.empArray.add(new Employee()); } 
  2. The way you are passing employees to the list to be added will not compile in java. Here is a good tutorial on how to create objects in java. In fact it seems you are new to Java, so I recommend you start on page one of the tutorials, they are very good for getting familiar with the language fast and you will be productive in no time.

From static method you have access only to static fields.
From not-static method you have access to all fields.
Eg

private int i = 0;
private static int j = 0;

public void increment()
{
   i++; // correct
   j++; // correct
}
public static void staticIncrement()
{
   i++; // compilation error
   j++; // correct
}  

So, in you example empArray should be static .

public class ClientClass
{
    static ArrayList<EmployeeClass> empArray = new ArrayList<EmployeeClass>(); 

    public static void main (String[] args)
    {
       int objcount = 0;   //variable to store objct count

       empArray.add(objcount, new EmployeeClass());
       empArray.add(new EmployeeClass());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM