4

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)

4
  • 2
    Put a static in front of the declaration of the empArray variable. Like this: static ArrayList<EmployeeClass> empArray = new ... Commented Oct 22, 2013 at 20:30
  • 2
    empArray is a member of the class, main is an static member, so, this is forbidden Commented Oct 22, 2013 at 20:30
  • 2
    static method cannot access non-static member. Commented Oct 22, 2013 at 20:32
  • Ok it worked. Nice.! But I'm still confused about the concept here. Sorry guys, please don't be furious :-P Commented Oct 22, 2013 at 20:34

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the elaboration. But as for 2nd point, compiler is compiling my code without any error. I also don't see any problem with my declaration, because in plain words, I'm creating ArrayList which is of type "EmployeeClass". Why shouldn't it? As I've read, ArrayList<T> => where T is template (can accommodate any data type).
2

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

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());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.