Static Import in java with examples| Difference between static vs normal import

This tutorials shows you static import feature.

Static Import introduced in Java 1.5 version

Static import is a Java language feature introduced in version 5, code-named as tiger version. Its purpose is to simplify the access of static variables and members without using class names.

Before the introduction of static import, normal import statements were used for every Java class to access methods and classes with the class name. For example, a normal import for the List class looked like this

import java.util.List

How Java Code Looked Before the Static Import Feature In traditional Java coding, to use any static member variables or a method of a class, you had to first import that class using the normal import keyword. After that, variables/methods were used with the class name in the code (e.g., Math.E).

Below is an example of normal import usage accessing member variables using a qualified class name:

import java.lang.*;
public class ImportDemo {
  public static void main(String args[]) {
    System.out.println("This is a sample program without
     using static import functionality" + Math.E);
  }
}

In this program,

  • The System and Math classes from the java.lang package are used.
  • In Java programs, there’s no need to explicitly import the java.lang package, as it is imported by default.
  • The java.lang package is imported for tutorial purposes.

static import example

Now, let’s see an example of the static import feature. With static import, you can use static member variables of java.lang.System and java.lang.Math classes without a qualified class name.

import static java.lang.System.*;
import static java.lang.Math.*;
public class ImportDemo {
  public static void main(String args[]) {
    out.println("This is a sample program without
     using static import functionality" + E);
  }
}

Here, out is a static field in the java.lang.System class, and E is a static member variable for the java.lang.Math class.

It is a recommended to define for each static member(Math.E) instead of all static members(Math.*) in the static import declaration.

Static Import Advantages

  • It allows calling static members without using the class name in the code.
  • Code statements with static import make all static members of that class available to declare in a Java class.
  • Static import can be used for static member variables as well as static members without referring to the class name, improving code readability for constants.
  • It simplifies the code readability for the constants.

Static Import Disadvantages

If two same static constants are imported from different classes in the same class, there’s a possibility of ambiguity, leading to a compiler error.

Difference Between Static Import and Normal Import

The fundamental difference is that normal import allows calling member variables using the class name in the package, while static import allows defining and using static member fields directly without referring to any class name.

Static ImportNormal Import
Improved readabilityClean code
No performance improvementsNo performance improvements
Only applies to static membersApplies to all types

Conclusion

In conclusion, we’ve learned about the static import feature introduced in Java 1.5. This syntax improvement enhances code readability and cleanliness. Additionally, we’ve posted the advantages and disadvantages and compared this with a normal import example.