Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static import statements which should never be after nonstatic imports

Codenarc is a framework that analyzes Groovy code for defects, bad practices, inconsistencies, style issues and more.

Is there a reason for this rule: MisorderedStaticImports Rule:

Checks for static import statements which should never be after nonstatic imports.

Examples of violations:

import my.something.*
import static foo.bar.*

public class MyClass{}

I don't understand the motivation or implications for this rule.

like image 753
Arturo Herrero Avatar asked Nov 14 '12 09:11

Arturo Herrero


People also ask

Why should we avoid static imports?

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.

Can we use static after import 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 is static import statement 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.

Which is true about the static import statement in Java?

1) It is used when one wants to use a class's static members. 2)It is the same as import statement 3) when static import statement is used in a java source file,the classes declared in that file cannot declare any non-static members. 4)The statement can be written either as static import of import static.


1 Answers

As Marko Topolnik says, the order of the imports is not relevant to the meaning of a program.

I tried looking at the JLS for an explicit statement to that effect, but I couldn't find one. And the Java Tutorial doesn't mention import order either. However, the semantics of import are such that it makes no difference. If the imports result in any potential ambiguity due to import order, the JLS says it is a compilation error.

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!)


UPDATE

@Eugene Stepanenkov points out this Q&A

  • Why do I get different compilation result depending on java imports and static imports sequence order?

That was caused by an obscure bug in versions of the Oracle Java compiler prior to Java 8. It was (eventually) acknowledged as a bug and fixed.

I guess, that means that the bug could have been part of the motivation for the Codenarc warning. However if you were using a Java compiler affected by the bug, then any code with the imports in the "wrong" order would not compile at all ... rendering the Codenarc warning moot.

like image 148
Stephen C Avatar answered Sep 19 '22 02:09

Stephen C