Apply a non-static method in a static context

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
stripes
Messages
262
Reaction score
0

Homework Statement



Very simple problem; I have three classes, Company, CompanyTest, and Employee. CompanyTest gets a bunch of information together about an employee, like salary, address, etc., and then it creates an Employee object using the Employee class. Great, so I have successfully asked the user for information, and an Employee object, let's call it emp, has been created.

Now in Company class, I have an ArrayList (obviously dynamic) that will keep a list of all the employees. In Company, I also have the following method:

Code:
public ArrayList<Employee> list = new ArrayList<Employee>();
	
	public void addEmployee(Employee person)
	{
		list.add(person);
	}

Finally, in CompanyTest, I want to add this object emp by saying

Code:
Company.addEmployee(emp);

in CompanyTest. So I'm trying to add emp to the list of employees, and each Employee object will be created in Employee class.

I keep getting the error:

Code:
CompanyTest.java:49: non-static method addEmployee(Employee) cannot be referenced from a static context
				Company.addEmployee(emp);
                                       ^
1 error

So my question is: how do I add an employee to this list that is created in Company class? This is getting very annoying as it is a very simple task that a computer can't seem to get.

Thanks in advance.

Homework Equations





The Attempt at a Solution

 
on Phys.org
Basically I'm trying to figure out how to call a method that uses an ArrayList.
 
How, exactly, are you using this? You appear to be defining a type "company" that contains an employee list. But that means that a specific company has a employee list. You cannot access an employee list from the "company" type. That is what is meant by a "non-static" method. You would have to declare a variable like:
Company FredsComputerRepair;

and then FredsComputerRepair.addEmployee(Harold).