Apply a non-static method in a static context

Click For Summary
SUMMARY

The discussion addresses the error encountered when trying to call a non-static method, addEmployee(Employee), from a static context in Java. The user attempts to add an Employee object to an ArrayList within the Company class using Company.addEmployee(emp), which results in a compilation error. The solution involves creating an instance of the Company class, such as Company myCompany = new Company();, and then calling the method on that instance: myCompany.addEmployee(emp);.

PREREQUISITES
  • Understanding of Java class structures and object-oriented programming
  • Familiarity with static vs. non-static methods in Java
  • Knowledge of Java collections, specifically ArrayList
  • Basic experience with Java error handling and debugging
NEXT STEPS
  • Learn about Java instance vs. static methods and their use cases
  • Explore Java collections framework, focusing on ArrayList operations
  • Study object-oriented design principles in Java
  • Practice debugging Java code to resolve common compilation errors
USEFUL FOR

Java developers, computer science students, and anyone learning object-oriented programming concepts in Java.

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

 
Physics news 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).
 

Similar threads

Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 8 ·
Replies
8
Views
7K