我有一个看起来像这样的课:
package module_4;
import java.util.Scanner;
public class monthPrinter {
// just does not need to be public
private static final String[] DAYS = {"Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"};
// will be used by the other program that loops through all the months of a year so needs to be public
public static final String[] MONTH_ARRAY = new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
}
我有另一个正在尝试使用此类的类,如下所示:
package module_4;
public class monthPrinterInYear {
public static void main(String args[]) {
for (String month : monthPrinter.MONTH_ARRAY) {
System.out.println(month);
}
}
}
我的编译器错误如下:
javac monthPrinterInYear.java
monthPrinterInYear.java:5: error: cannot find symbol for (String month : monthPrinter.MONTH_ARRAY) {
^
symbol: variable monthPrinter
location: class monthPrinterInYear
1 error
我在做什么错?我想访问该MONTH_ARRAY
常量,但似乎无法理解。
答案 0 :(得分:0)
修正您的命名约定以遵循标准。确保您的源代码位于编译器期望的适当相对位置。您是否按包名称正确命名了子文件夹?
答案 1 :(得分:0)
您不是在编译monthPrinter
,而是在尝试编译monthPrinterInYear
,这很麻烦。只需将monthPrinter.java
和monthPrinterInYear.java
都保留在名为module_4
的同一目录中,然后导航到该目录并运行命令:
javac *.java
这将尝试编译该目录中的所有Java文件。这很重要-运行编译的文件,向后浏览一个文件夹,即导航到保留module_4
的父目录并发出命令:
java module_4.monthPrinterInYear