0

I have a java file which contains a list of contents that I'm looking to import statically into another file.

However, I am running into an error that the compiler "cannot find symbol".

Visual code has also highlighted the code with the error:

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

I followed the syntax from this 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;
...
}

I'm running Visual Code with Red hat's language support for Java on JDK 9.

Sudha Velan
  • 633
  • 8
  • 24
Carrein
  • 3,231
  • 7
  • 36
  • 77
  • 6
    "static imports are only available if source level is 1.5 or greater." so bump your level to 1.6 1.7 or 1.8 –  Sep 09 '17 at 06:45
  • see https://stackoverflow.com/questions/1736730/eclipse-magic-syntax-error-varargs-are-only-available-if-source-level-is-1 for example (see also https://stackoverflow.com/search?q=%22only+available+if+source+level+is+1.5+or+greater.%22) –  Sep 09 '17 at 06:45
  • This would possibly be a duplicate of one of those listed by RC. – Naman Sep 09 '17 at 06:49
  • Run `javac -version` and paste the output – jrtapsell Sep 09 '17 at 07:25

1 Answers1

0

You can't import from the default package.

Give your Constants class a package declaration:

package something;

The import using

import static something.Constants.*;
Andy Turner
  • 137,514
  • 11
  • 162
  • 243