I am trying to pass a reference of an enum type from a constructor to test some lambdas, method reference, and Stream
s. When I try to instantiate the class I am getting a error on the enum.
public class Book {
private String title;
private List<String> authors;
private int pageCount[];
private Year year;
private double height;
private Topic topic;
public Book (String title, ArrayList<String> author, int pageCount [], Year year, double height, Topic topic )
{
this.title = title;
this.authors = author;
this.pageCount = pageCount;
this.topic = topic;
this.year = year;
this.height = height;
}
public enum Topic
{
MEDICINE, COMPUTING, FICTION, HISTORY
}
public String getTitle(){
return title;
}
public List<String> getAhuthors(){
return authors;
}
public int [] getPageCounts(){
return pageCount;
}
public Topic getTopic()
{
return topic;
}
public Year getPubDate(){
return year;
}
public double getHeight()
{
return height;
}
public static void main(String args [] )
{
Book nails = new Book("Fundamentals of Chinese Fingernail image", Arrays.asList("Li", "Fun", "Li"),
new int [] {256}, Year.of(2014), 25.2, COMPUTING);
Topic test = Topic.COMPUTING;
System.out.println(test);
}
}
this is what I am getting :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
COMPUTING cannot be resolved to a variable at Book.main(Book.java:70)
Replace
Book nails = new Book("Fundamentals of Chinese Fingernail image", Arrays.asList("Li", "Fun", "Li"),
new int [] {256}, Year.of(2014), 25.2, COMPUTING);
by
Book nails = new Book("Fundamentals of Chinese Fingernail image", Arrays.asList("Li", "Fun", "Li"),
new int [] {256}, Year.of(2014), 25.2, Topic.COMPUTING);
EDIT:
Like @Voltboyy pointed out, you must change qhat i pointed out before and replace the ArrayList<String>
in the constructor to List<String>
like this:
public Book(String title, List<String> list, int pageCount[], Year year, double height, Topic topic) {
this.title = title;
this.authors = list;
this.pageCount = pageCount;
this.topic = topic;
this.year = year;
this.height = height;
}
and you program will work.
As said by @djointster, COMPUTING
should be Topic.COMPUTING
.
But your code will still not work afterwards as private List<String> authors
differs from ArrayList<String> author
in your constructor.
So you should change this:
ArrayList<String> authors;
//to
List<String> authors;
in the constructor.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.