Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java static import causing compile error. Probable compiler bug?

Tags:

java

This compiles fine in Eclipse JDT but not on 1.6.30 or 1.7.25:

package doh;

import static doh.Wtf.InnerClass.innerclassMethod;
import java.io.Serializable;

public class Wtf {

    static class InnerClass implements Serializable {   
        public static void innerclassMethod() {            
        }
    }
}

With javac I get the following compile error:

error: cannot find symbol
  static class InnerClass implements Serializable {     
symbol:   class Serializable
location: class Wtf

Commenting out the superfluous static import makes the code compile. So does reordering the import statements.

like image 583
slackhacker Avatar asked Jul 24 '13 13:07

slackhacker


People also ask

Are static imports bad Java?

by Joshua Bloch.) This is considered bad Java programming practice because when a class implements an interface, it becomes part of the class's public API. Implementation details, such as using the static members of another class, should not leak into public APIs.

Can you import a static method in Java?

In Java, static import concept is introduced in 1.5 version. With the help of static import, we can access the static members of a class directly without class name or any object. For Example: we always use sqrt() method of Math class by using Math class i.e. Math.

What does static import mean in Java?

Static import is a feature introduced in the Java programming language that allows members (fields and methods) which have been scoped within their container class as public static , to be used in Java code without specifying the class in which the field has been defined.

Should static imports come first?

As Marko Topolnik says, the order of the imports is not relevant to the meaning of a program. Therefore, this is just a stylistic convention. (Indeed, if you look at the rule, it even gives you the option of putting the static imports first!)


1 Answers

I get the same compile error with jdk 1.7.25.

It seems to be a known bug (although the example given in the bug report uses an enum as the nested class but it is conceptually identical) and the proposed workarounds are the same as those you describe:

  • swap import statements
  • remove static import and use fully qualified name
like image 173
assylias Avatar answered Sep 18 '22 01:09

assylias