2017-09-09 52 views
0

我有一個java文件,其中包含我正在靜態導入到另一個文件中的內容列表。無法在Java中靜態導入類

但是,我遇到了編譯器「無法找到符號」的錯誤。

視覺代碼也突出了代碼的錯誤:

Syntax error, static imports are only available if source level is 1.5 or greater.

我跟着從這個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運行與Java的Red Hat的語言支持可視化代碼

+6

進口「靜態進口僅當源級別是1.5或更高。」所以碰撞你的水平1.6 1.7或1.8 –

+0

見https://stackoverflow.com/questions/1736730/eclipse-magic-syntax-error-varargs-are-only-available-if-source-level-is-1例如(另請參閱https://stackoverflow.com/search?q=%22only+available+if+source+level+is+1.5+or+greater.%22) –

+0

這可能與上面列出的其中一個重複RC。 – nullpointer

回答

0

您不能導入來自默認包。

給你的常量類包聲明:

package something; 

使用

import static something.Constants.*;