Apply a non-static method in a static context

In summary, the conversation discusses a problem with adding an employee to an ArrayList in the Company class. The error "non-static method addEmployee(Employee) cannot be referenced from a static context" is encountered and the question is how to resolve it. The solution involves calling the addEmployee method using a specific company's variable, rather than the "company" type.
  • #1
stripes
266
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
  • #2
Basically I'm trying to figure out how to call a method that uses an ArrayList.
 
  • #3
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).
 

1. What is a static context in Java?

A static context in Java refers to the state of a class or method that does not require an instance of the class to be created in order to be accessed. This means that the method or variable can be accessed directly through the class name without creating an object of that class.

2. Can non-static methods be used in a static context?

No, non-static methods cannot be used in a static context. This is because a non-static method requires an instance of the class to be created in order to be accessed, while a static context does not allow for the creation of instances.

3. How can a non-static method be applied in a static context?

In order to apply a non-static method in a static context, the method must be made static by adding the keyword "static" to the method declaration. This allows the method to be accessed without creating an instance of the class.

4. What is the purpose of applying a non-static method in a static context?

The purpose of applying a non-static method in a static context is to allow for the method to be accessed without creating an instance of the class. This can be useful in cases where the method does not require any instance variables and can be used globally without the need for creating objects.

5. Is it a good practice to apply non-static methods in a static context?

It is generally not considered a good practice to apply non-static methods in a static context. Non-static methods are meant to be used with instances of the class and making them static goes against the principles of object-oriented programming. However, there may be some cases where it is necessary to use non-static methods in a static context for convenience or efficiency purposes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
832
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top