New Reply

non-static variable method cannot be referenced from a static context

 
Share Thread
Jun30-12, 11:34 PM   #1
 

non-static variable method cannot be referenced from a static context


1. The problem statement, all variables and given/known data

I just need to know how to access an arraylist that was created in a class (call it Company). In class CompanyTest, I need to access a list of employees that was created in Company.

This is Company class:
Code:
	public ArrayList<Employee> list = new ArrayList<Employee>();
	
	public ArrayList<Employee> getList()
	{
		return list;
	}
and in CompanyTest class, I wish to access the list by calling the method getList:
Code:
public class CompanyTest extends Company
	
{	
	Company method = new Company();
	
	public static void main(String[] args)
	{
		ArrayList<Employee> list = method.getList();
                //code continues
        }
...

static class Action implements ActionListener
		{
			public void actionPerformed (ActionEvent e)
			{
				ArrayList<Employee> list = method.getList();

				//code continues

                                Employee emp = new Employee(tempName, tempAddress);
				list.add(emp);
			}
		}
So the idea here is that I create the Company object outside the main method so that the method getList can be used in the main method, and in other subclasses (the actionlistener one).

I keep getting a compile error saying "non-static variable method cannot be referenced from a static context"

What do I need to do to make the list (which is already public in Company class) accessible to whatever I want. Not a very complicated question and I think I may have asked it before. Mark44 I think has done his best to help me in the past but this problem is quite simple. My assignment is due in 2 and a half hours so if anyone reads this and is able to give me the quick answer, it would be greatly appreciated. Thanks everyone.
PhysOrg.com science news on PhysOrg.com

>> City-life changes blackbird personalities, study shows
>> Origins of 'The Hoff' crab revealed (w/ Video)
>> Older males make better fathers: Mature male beetles work harder, care less about female infidelity
Jul1-12, 01:47 AM   #2
 
Problem solved. Simply added static in front of the company object from Company class I think. Then I created a method companyPass to pass the Company object to wherever companyPass was located, and then used my existing getList method to extract the list from the Company object.

Much simpler than the tutorials I read online.
New Reply

Similar discussions for: non-static variable method cannot be referenced from a static context
Thread Forum Replies
Apply a non-static method in a static context Engineering, Comp Sci, & Technology Homework 2
Static force/Static friction Introductory Physics Homework 5
Static spacetime as static universe Cosmology 7
Is TV static ... Cosmology 12
Which direction do we assume the static friction force to be in? (Static equilibrium) Classical Physics 1