Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中导入类和常量时遇到问题_Java - Fatal编程技术网

在java中导入类和常量时遇到问题

在java中导入类和常量时遇到问题,java,Java,我有一个类似这样的类: 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 othe

我有一个类似这样的类:

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
我有另一个类正在尝试使用这个类,看起来像这样:

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
我的编译器错误如下所示:

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\u数组
常量,但我似乎无法访问它。

请按照标准修改命名约定。确保源代码位于编译器预期的适当相对位置。您是否按照软件包名称正确命名子文件夹?

您没有编译
monthPrinter
,而只是尝试编译
monthPrinterInYear
,这会造成问题。只需将
monthPrinter.java
monthPrinterInYear.java
保存在名为
module_4
的同一目录中,然后导航到该目录并运行以下命令:

javac *.java
这将尝试编译该目录中的所有java文件。重要的是,要运行编译后的文件,请向后导航一个文件夹,即导航到剩余
module_4
的父目录,并发出以下命令:

java module_4.monthPrinterInYear

请遵循标准命名约定。类名应以大写字母开头。您是否遵循源代码的标准布局?编译错误在哪里?您不需要从同一个包导入类。请更正您的问题以提供编译器错误声明静态导入仅适用于类和接口上的字段。这几乎是错误消息一字不差地告诉您的。您需要非静态导入。monthPrinter.java文件在哪里?与monthPrinter相同的包运行javac时的当前目录是什么?您需要在上面一级才能找到包文件夹。或者使用javac-sourcepath。。