Example of Static Import in Java

Java programming is used by many users worldwide. It provides numerous packages to solve different problems. To use the Java packages in our programs, import keyword is used. In this section, we will discuss about static import in Java.

Java import Keyword

Most of the Java programs starts with the statements having import keyword. It is similar to the preprocessor directives used in C or C++ programming. The import in Java is a keyword that allows the programmer to access packages available in Java. It is used to import a package, sub-package, a class, an interface or enum in the Java program.

Syntax:

Example of Static Import in Java

The following Java program demonstrates the use of import statement.

ExampleImport.java

Output:

Demonstrating use of import statement in Java
Tue Jul 20 18:42:43 UTC 2021

In the above code, the java.util package is used in the program and it is included using the import statement at the start of the program.

Java Static Import

The Java static import was introduced in JDK version 1.5. With the help of static import, the static variables and methods of imported classes can be accessed. We don't need to specify the class name or object name to access the methods or variables. Using a static import statement saves time because the programmer isn't required to use the class name or object name again and again.

Syntax:

Example of Static Import in Java

Program before using the static import statement

The following program shows the use of the methods of the Math class without using the static import statement.

ExampleImport1.java

Output:

Square root of 9 = 3
Result of 2 * pi = 6.283185307179586

In the above Java code, the static methods sqrt(), round() and variable PI of the Math class are accessed directly by using the class name.

Program after using the static import statement

The following program shows the use of methods of the Math class with the use of the static import statement.

ExampleStaticImport.java

Output:

Square root of 9 = 3
Result of 2 * pi = 6.283185307179586

In the above Java code, the Math class is imported using static import. The methods sqrt(), round() and variable PI of Math class are directly accessed without using the class name.

How static import statement is used?

1. Importing all data members using an asterisk (*):

When the programmer wants to use all the static data members of the class, it can be done in the following way.

ExmapleStaticImport.java

Output:

Minimum value of an Integer: -2147483648
Maximum value of an Integer: 2147483647

In the above Java code, data members of the Integer class are imported using an asterisk symbol that denotes all the static members that can be used in the program without specifying the class name.

2. mporting static variables by names:

The situation where the Java program only requires the specific static variable to be used. It can be imported in the following manner.

ExampleStaticImport2.java

Output:

2 * PI = 6.283185307179586

In the above Java code, the static variable PI of the Math class is only used.

3. Importing static methods by names:

The situation where the Java program only requires the specific static method to be used. It can be imported in the following manner.

ExampleStaticImport3.java

Output:

Square root of a = 4.0

In the above Java code, the sqrt() method of the Math class is only used.

Advantages of static import

  • When a program demands the use of different static members from one or two classes, it is a good choice to use a static import statement instead of using the class name again and again.
  • It saves the time.

Disadvantages of static import

  • Overuse of static import may lead to less readable code. The maintainability of code is also decreased as identifying which class the static member belongs will be difficult.
  • Use of static import may also cause the problem of ambiguity. Static members with the same name from two different classes cannot be distinguished. And hence the compiler will throw an error message.