AvoidStaticImport
Since Checkstyle 5.0
Description
Rationale: Importing static members can lead to naming conflicts between class' members. It may lead to poor code readability since it may no longer be clear what class a member resides in (without looking at the import statement).
Notes
If you exclude a starred import on a class this automatically excludes each member individually.
For example: Excluding java.lang.Math.*
. will allow the
import of each static member in the Math class
individually like java.lang.Math.PI, java.lang.Math.cos, ...
.
Properties
name | description | type | default value | since |
---|---|---|---|---|
excludes | Control whether to allow for certain classes via a star notation to be excluded such as java.lang.Math.* or specific static members to be excluded like java.lang.System.out for a variable or java.lang.Math.random for a method. See notes section for details. |
String[] | {} |
5.0 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="AvoidStaticImport"/>
</module>
</module>
Example:
import static java.lang.Math.pow; // violation
import static java.lang.System.*; // violation
import java.io.File; // OK
import java.util.*; // OK
To configure the check so that the java.lang.System.out
member and all members from java.lang.Math
are allowed:
<module name="Checker">
<module name="TreeWalker">
<module name="AvoidStaticImport">
<property name="excludes" value="java.lang.System.out,java.lang.Math.*"/>
</module>
</module>
</module>
Example:
import static java.lang.Math.*; // OK
import static java.lang.System.out; // OK
import static java.lang.Integer.parseInt; // violation
import java.io.*; // OK
import java.util.*; // OK
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.imports