2017-09-09 51 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.*;