I’m new to Java so I was trying LeetCodes arrays lesson and I copied the code directly from the page and it returned an error in there own IDE does anyone know what is causing this?
Thanks.
DVD[] dvdCollection = new DVD[15];
// A simple definition for a DVD.
public class DVD {
public String name;
public int releaseYear;
public String director;
public DVD(String name, int releaseYear, String director) {
this.name = name;
this.releaseYear = releaseYear;
this.director = director;
}
public String toString() {
return this.name + ", directed by " + this.director + ", released in " + this.releaseYear;
}
}
>Solution :
You must declare the array within a class
// A simple definition for a DVD.
public class DVD {
public String name;
public int releaseYear;
public String director;
//Move declaration within class
DVD[] dvdCollection = new DVD[15];
public DVD(String name, int releaseYear, String director) {
this.name = name;
this.releaseYear = releaseYear;
this.director = director;
}
public String toString() {
return this.name + ", directed by " + this.director + ", released in " + this.releaseYear;
}
}