【发布时间】:2019-08-27 13:15:19
【问题描述】:
我有一个程序有两个声明为单音类的类。 当我尝试运行程序时,我得到一个 java.lang.Error: Unresolved compiler problem 在获取单音类实例的行中,我不明白为什么。
错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at Model.Recommender.getInstance(Recommender.java:22)
at Model.Library.<clinit>(Library.java:21)
at Controller.MainClass.main(MainClass.java:69)
Recommender 类中的错误行:
public static Recommender getInstance() {
而类是这样声明的:
public class Recommender {
private static Recommender recommender; //a recommender
private HashSet<Reader> libraryReaders; //a hash set of all of the library readers
//constructor to initialize the fields
private Recommender()
{
libraryReaders = new HashSet<Reader>();
}
//method to make the class a singleton class
public static Recommender getInstance() {
if(recommender == null) //check that a instance of this class doesn't exist
{
recommender = new Recommender(); //creates a new instance
}
return recommender; //return the instance that was created
}
}
库类中的错误行:
private static Recommender recommender = Recommender.getInstance();
而类是这样声明的:
public class Library {
private static Library library; //a library
private static Recommender recommender = Recommender.getInstance();
private HashSet<Reader> readers;
private HashSet<Author> authors;
private HashSet<LibraryItem> items;
//constructor to initialize the fields
private Library() {
readers = new HashSet<Reader>();
authors = new HashSet<Author>();
items = new HashSet<LibraryItem>();
}
//method to make the class a singleton class
public static Library getInstance() {
if(library == null) //check that a instance of this class doesn't exist
{
library = new Library(); //creates a new instance
}
return library; //return the instance that was created
}
}
Main 类中的错误:
LibrarySys = Library.getInstance();
我正在库类中创建一个推荐器实例,并在主类中创建一个库实例。为什么会出现此错误?
【问题讨论】:
-
您的方法名称是
Recommender.getInstance(),您调用的是Recommender.getInstance1()。我认为只是一个拼写错误;) -
我改了,还是一样的错误
-
@Daniel16 我无法重现您的问题,我复制了相同的代码,但没有得到任何东西。您能否添加更多信息,例如包和导入名称?
-
注意:“未解决的编译问题”表示代码无法正确编译。当 Eclipse 在代码中显示错误时,您无法干净地运行代码。
标签: java eclipse compiler-errors