static import only from classes and interfaces

Carl Manaster picture Carl Manaster · Aug 2, 2012 · Viewed 30.1k times · Source

My code compiles fine in Eclipse, but when I try to compile from the commandline (via our ruby-based buildr system), I get this error message:

static import only from classes and interfaces

Suggesting that static import of public static fields is not permitted. What should I look for to help diagnose this problem? How can I fix it?

Update: per @Ted's request, the constant declaration in the referenced file:

public static final String NULL = "<NULL>";

and the (bowdlerized) reference in the referring file:

import static my.path.MyClass.NULL;

Answer

Ted Hopp picture Ted Hopp · Aug 2, 2012

My guess is that Eclipse and buildr are using either different Java compiler versions or different compiler flags. There's a bug in the Java 7 compiler (bug ID: 715906) that generates this error when you statically import specific fields. The work-around is to use a wildcard static import. So instead of:

import static pkg.Class.staticField;

do this:

import static pkg.Class.*;