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

Click For Summary
SUMMARY

The discussion addresses the compile error "non-static variable method cannot be referenced from a static context" encountered when trying to access a non-static ArrayList from a static method in Java. The user created a Company class with a public ArrayList and a method getList() to retrieve it. The solution involved declaring the Company object as static, allowing access to the getList() method within the static context of the main method and other static classes. This approach simplifies the process of accessing instance variables from static methods.

PREREQUISITES
  • Understanding of Java class structure and object-oriented programming principles
  • Familiarity with ArrayList and its methods in Java
  • Knowledge of static vs. non-static context in Java
  • Basic experience with event handling in Java using ActionListener
NEXT STEPS
  • Review Java static and non-static member variables and methods
  • Learn about Java ArrayList and its usage in data management
  • Explore Java event handling and the role of ActionListener
  • Investigate best practices for object-oriented design in Java
USEFUL FOR

Java developers, computer science students, and anyone learning object-oriented programming concepts in Java, particularly those facing issues with static and non-static contexts.

stripes
Messages
262
Reaction score
0

Homework Statement



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
	
{	
	[B][SIZE="3"]Company method = new Company();[/B]
	
	public static void main(String[] args)
	{
		[B][SIZE="3"]ArrayList<Employee> list = method.getList();[/B]
                //code continues
        }
...

static class Action implements ActionListener
		{
			public void actionPerformed (ActionEvent e)
			{
				[B][SIZE="3"]ArrayList<Employee> list = method.getList();[/B]

				//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.
 
Last edited:
Physics news on Phys.org
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.
 

Similar threads

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