无法在Java中静态导入类

时间:2017-09-09 06:41:02

标签: java static

我有一个java文件,其中包含我希望静态导入另一个文件的内容列表。

但是,我遇到一个错误,编译器"找不到符号"。

可视代码也突出显示了错误代码:

  

语法错误,静态导入仅在源级别为1.5时可用   或更高。

我遵循了post的语法。

Constants.java

public final class Constants{
    private Constants(){} //Private constructor - no instantiation or subclassing.

    // The first two members are constants, used to configure the simulator.
    public static final int MAX_NUMBER_OF_EVENTS = 100; // Maximum number of events 
    public static final double SERVICE_TIME = 1.0; // Time spent serving a customer
    public static final int CUSTOMER_ARRIVE = 1;
    public static final int CUSTOMER_DONE = 2;
}

Simulator.java

import static Constants.*; //doesn't work.

public class Simulator{   
    private Event[] events = new Event[MAX_NUMBER_OF_EVENTS]; //Array to queue events - order of events not guaranteed.

    //numOfEvents keeps track of number of events in the array.
    //total* variables used to track simulation.
    //*Id variables used to identify customer.
    private int numOfEvents, totalNumOfServedCustomer, totalNumOfLostCustomer =  0;
    private int lastCustomerId = 0;
    private int servedCustomerId, waitingCustomerId = -1;

    //booleans used to track customer status i.e. served or waiting.
    private boolean customerBeingServed, customerWaiting = false;

    //doubles used to keep track of time of total simulation and waiting time.
    private double timeStartedWaiting, totalWaitingTime = 0;
...
}

我在JDK 9上使用Red Hat的Java语言支持运行Visual Code。

1 个答案:

答案 0 :(得分:0)

您无法从默认包中导入。

为您的Constants类提供包声明:

package something;

使用

导入
import static something.Constants.*;