Importing static
members of a class or interface.
Besides the capability to import classes or interfaces, enabling the compiler to resolve simple-name-only references to those classes or interfaces, there’s another type of import
statement available to us.
Many classes and interfaces declare one or more static
members. Usually, we reference such an element using the class name, followed by the name of the static
field or method. For example, the following code references two static
members of the java.lang.Math
class: the PI
constant and the round
method:
long roundedPi = Math.round(Math.PI);
(Note that we don’t need to import the java.lang.Math
class in order to reference it by its simple name, since the Java compiler automatically imports all classes and interfaces in the java.lang
package.)
Alternatively, we could start by including the following import static
statements in the source code file:
import static java.lang.Math.PI;
import static java.lang.Math.round;
With those imports in place, we could rewrite the previous references to PI
and round
like this:
long roundedPi = round(PI);
As you’ve now probably figured out, the import static
statement permits us to refer to a static
member without preceding it with the class or interface name. This can be convenient; however, it can also make our code less self-documenting. Thus, import static
should be used with caution.
Like the non-static
type of import
statement, any import static
statements must appear after the package
statement (if present), and before any class or interface declarations in the file.
import static {packagename[.subpackagename[...]].ClassOrInterfaceName.staticMemberName};
(Note that the braces and brackets are part of the placeholders, and must not appear in the actual import static
statement.)
packagename
The root-level package name.
subpackagename
Zero or more nested levels of subpackage names.
ClassOrInterfaceName
Simple name of the class or interface from which a static
member will be imported.
staticMemberName
Name of static
field or method to import.
Note that the import static
statement must be terminated with a semicolon. Also, remember that the braces and brackets are part of the placeholders, and must not appear in the actual import
statement.
import static java.lang.Math.E;
import static java.lang.Integer.parseInt;
This statement instructs the compiler to recognize any references to E
or parseInt
—the first a constant in the java.lang.Math
class, and the second a method in the java.lang.Integer
class—as references to the respective fully-qualified constant and method (respectively). Thus, the following rather silly code would be allowed (and properly handled by the compiler) in the code of a class or interface definition in the source file containing those import static
statements:
double solutionProbability = parseInt("1") / E;
import static {packagename[.subpackagename[...]].ClassOrInterfaceName}.*;
(Again, the braces and brackets are part of the placeholders, and must not appear in the actual import static
statement.)
packagename
The root-level package name.
subpackagename
Zero or more nested levels of subpackage names.
ClassOrInterfaceName
Simple name of the class or interface from which all static
members will be imported.
Again, the import static
statement must be terminated with a semicolon.
import static java.lang.Math.*;
This statement instructs the compiler to scan the contents of the java.lang.Math
class, and to recognize references to static
members of that class (the PI
and E
constants, trigonometric methods, logarithmic methods, etc.), without requiring that they be prefaced with Math.
.
With one notable exception (described in the style guide), the use of import static
in Java source files is not permitted in this bootcamp.