In Java, the import statements imports classes from packages, so they can be used in the current class without the package reference. Similarly, the static import statements import the static members of the a class and allows to be used without class reference.
1. Types of Static Import Statements
A static import declaration comes in two flavors:
- single-static import: imports one static member (field or method) from a class.
- static-import-on-demand: imports all static members from a class. These are denoted with an asterisk (*).
1.1. Syntax
The general syntax of static import statements is as follows:
import static <<package name>>.<<type name>>.<<static member name>>;
import static <<package name>>.<<type name>>.*; //On-demand
1.2. Example
For example, we can print the messages in the standard output using the System.out.println()
method. System
is a class in java.lang package that has a static variable named out
.
When we use System.out
, we are referring to that static variable out of the System
class. We can use a static import statement to import the out
static variable from the System
class as follows:
import static java.lang.System.out;
The code can now use the name out to mean System.out
in the program. The compiler will use the static import declaration to resolve the name out
to System.out
.
public class StaticImportTest {
public static void main(String[] args) {
out.println("Hello World!");
}
}
2. Rules for Static Imports
The following are some important rules about static import statements.
2.1. Specifically Imported Member Preceeds Wildcard Imported Member
If two static members with the same simple name are imported, one using single-static import declaration and the other using static-import-on-demand declaration, the one imported using single-static import declaration takes precedence.
Suppose there are two classes, package1.Class1
and package2.Class2
. Both classes have a static method called methodA
. The following code will use package1.Class1.methodA()
method because it is imported using the single-static import declaration:
import static package1.Class1.methodA; // Imports Class1.methodA() method
import static package2.Class2.*; // Imports Class2.methodA() method too
public class Test {
public static void main(String[] args) {
methodA(); // Class1.methodA() will be called
}
}
2.2. Static Member Names should be Unique
Using single-static-import declaration to import two static members with the same simple name is not allowed. The following static import declarations generate an error because both of them import the static member with the same simple name of methodA
:
import static package1.Class1.methodA;
import static package1.Class2.methodA; // An error
2.3. Static Member Defined in the Current Class Takes Precedence
If a static member is imported using a single-static import declaration and there exists a static member in the same class with the same name, the static member in the class is used.
package package1;
public class A {
public static void test() {
System.out.println("package1.A.test()");
}
}
package package2;
import static package1.A.test;
public class Test {
public static void main(String[] args) {
test(); //package2.Test.test()
}
public static void test() {
System.out.println("package2.Test.test()");
}
}
3. Conclusion
The static imports help us use simple names of static members to make the program simpler to write and read. These are more of a syntactical sugar rather than providing any execution benefits in runtime.
Happy Learning !!