Static Import Declarations in Java

The normal import declaration imports classes from packages, so that they can be used without package reference. Similarly the static import declaration imports static members from classes and allowing them to be used without class reference.

A static import declaration also comes in two flavors: single-static import and static-import-on-demand. A single-static import declaration imports one static member from a type. A static-import-on-demand declaration imports all static members of a type. The general syntax of static import declaration is as follows:

//Single-static-import declaration:

import static <<package name>>.<<type name>>.<<static member name>>;

//Static-import-on-demand declaration:

import static <<package name>>.<<type name>>.*;

Static import example

For example, You remember printing 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 you use System.out, you are referring to that static variable out of the System class. You can use a static import declaration to import the out static variable from the System class as follows:

import static java.lang.System.out;

You code can now use the name out to mean System.out in your 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 static import!");
        }
}

Static import rules

The following are some important rules about static import declaration.

1) If two static members with the same simple name are imported, one using single-static import declaration and 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) 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

3) 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.

// A.java
package package1;
 
public class A {
        public static void test() {
                System.out.println("package1.A.test()");
        }
}
 
// Test.java
package package2;
 
import static package1.A.test;
 
public class Test {
        public static void main(String[] args) {
                test(); // Will use package2.Test.test() method, not package1.A.test() method
        }
 
        public static void test() {
                System.out.println("package2.Test.test()");
        }
}

Output:

package2.Test.test()

It may seem that static imports help you use simple names of static members to make the program simpler to write and read. Sometimes static imports may introduce subtle bugs in your program, which may be hard to debug. You are advised not use static imports at all, or only in very rare circumstances.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Static Import Declarations in Java”

  1. Hello Lokesh,

    Excellent work…I feel.

    I have come across your blog while looking (&googling) System class on grepcode.
    And also noticed your article “…why finalize method should not be used….”; & “…best way to learn core java …”

    Very nice. I thin it needs few improvements (updating the main text of article with few more line taken from your valuable responses….particularly in your post “why not to use finalize method”),

    adding key points, in the top of the article (eg: ” ….static imports are not recommended to use) besides sating in conclusion at the end of the article

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.