Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static import only from classes and interfaces

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; 
like image 621
Carl Manaster Avatar asked Aug 02 '12 16:08

Carl Manaster


People also ask

Which is true about the static import statement in Java?

This is Expert Verified Answer The static import feature of Java 5 facilitate the java programmer to access any static member of a class directly. There is no need to qualify it by the class name.

Why static imports are not good?

If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from.

What is the advantage of static import in Java?

The import allows the java programmer to access classes of a package without package qualification. The static import feature allows to access the static members of a class without the class qualification.


1 Answers

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.*; 
like image 194
Ted Hopp Avatar answered Oct 09 '22 04:10

Ted Hopp