Java 这个列表不会编译

Java 这个列表不会编译,java,linked-list,Java,Linked List,我的文件无法编译我一直收到一条消息说: 此处的main方法中的“无法对非静态字段employeeList进行静态引用” 我做错了什么 LinkedList数据应该是String还是Employee public class TrainingCourses { /* this is the list of employees * */ private LinkedList<Employee> employeeList; 公共课堂培训课程{ /*这是员工名单 * */ 私人Lin

我的文件无法编译我一直收到一条消息说:

此处的main方法中的“无法对非静态字段employeeList进行静态引用”

我做错了什么

LinkedList数据应该是String还是Employee

public class TrainingCourses {
/* this is the list of employees 
 * */
private LinkedList<Employee> employeeList; 
公共课堂培训课程{
/*这是员工名单
* */
私人LinkedList员工列表;

您正在从静态方法中调用实例方法和字段

改变这个

private LinkedList<Employee> employeeList; 
private LinkedList employeeList;
为此:

private static LinkedList<Employee> employeeList; 
private静态链接列表employeeList;

您正在访问
员工列表
,而没有创建
培训课程的对象

TrainingCourses objTrainingCourses  = new TrainingCourses ();
objTrainingCourses.employeeList(new Employee(i));

或者将
employeeList
static变量设置为您的Trainingcourses类包含一个LinkedList字段employeeList,但没有允许Trainingcourses对象的用户访问或更改包含的链接列表状态的实例(非静态)方法

虽然其他人在这里建议的一种解决方案是使LinkedList保持静态,但也许更好的解决方案是使Trainingcourses成为一个OOP兼容类,方法是重新考虑您的设计,并为Trainingcourses类提供一些实例方法和可能的字段


然后,您可以创建一个Trainingcourses实例并调用其方法。

如果您在谷歌上搜索异常“”的文本,将有很多资源,包括一些资源。您的employeeList方法不是静态的,这就是您收到此消息的原因,请将其设置为静态。。