Open In App

Static import in Java

Improve
Improve
Like Article
Like
Save
Share
Report

In Java, static import concept is introduced in 1.5 version. With the help of static import, we can access the static members of a class directly without class name or any object. For Example: we always use sqrt() method of Math class by using Math class i.e. Math.sqrt(), but by using static import we can access sqrt() method directly. 
According to SUN microSystem, it will improve the code readability and enhance coding. But according to the programming experts, it will lead to confusion and not good for programming. If there is no specific requirement then we should not go for static import.

Advantage of static import:

If user wants to access any static member of class then less coding is required.

Disadvantage of static import:

Static import makes the program unreadable and unmaintainable if you are reusing this feature.
 

JAVA




// Java Program to illustrate
// calling of predefined methods
// without static import
class Geeks {
    public static void main(String[] args)
    {
        System.out.println(Math.sqrt(4));
        System.out.println(Math.pow(2, 2));
        System.out.println(Math.abs(6.3));
    }
}


Output:  

2.0
4.0
6.3 

JAVA




// Java Program to illustrate
// calling of predefined methods
// with static import
import static java.lang.Math.*;
class Test2 {
    public static void main(String[] args)
    {
        System.out.println(sqrt(4));
        System.out.println(pow(2, 2));
        System.out.println(abs(6.3));
    }
}


Output: 

2.0
4.0
6.3

JAVA




// Java to illustrate calling of static member of
// System class without Class name
import static java.lang.Math.*;
import static java.lang.System.*;
class Geeks {
    public static void main(String[] args)
    {
        // We are calling static member of System class
        // directly without System class name
        out.println(sqrt(4));
        out.println(pow(2, 2));
        out.println(abs(6.3));
    }
}


Output: 

2.0
4.0
6.3

NOTE : System is a class present in java.lang package and out is a static variable present in System class. By the help of static import we are calling it without class name.
 

Ambiguity in static import:
If two static members of the same name are imported from multiple different classes, the compiler will throw an error, as it will not be able to determine which member to use in the absence of class name qualification. 

JAVA




// Java program to illustrate
// ambiguity in case of
// static import
import static java.lang.Integer.*;
import static java.lang.Byte.*;
public class Geeks {
    public static void main(String[] args)
    {
        system.out.println(MAX_VALUE);
    }
}


Output: 

Error:Reference to MAX_VALUE is ambiguous

Explanation : In the above program, we are trying to access MAX_VALUE variable, but Every primitive data type contains MAX_VALUE variable which is pre-declared in there Wrapper class. Here we are importing Integer and Byte class simultaneously and trying to access static variable MAX_VALUE but here compiler will be confused by seeing two import statements because both Integer and Byte class contains a static variable MAX_VALUE. Therefore here compiler throw an error saying Reference to MAX_VALUE is ambiguous.
NOTE : Two package contains two class/interface with the same name is very rare. Therefore it is very rare that we will get any ambiguity while importing normal way i.e. normal import. But it is possible that two classes contains same variable, therefore it is very common in static import to get error saying reference is ambiguous. Thats why it is not recommended to use if there is no such requirement. 
 

Difference between import and static import:

  • With the help of import, we are able to access classes and interfaces which are present in any package. But using static import, we can access all the static members (variables and methods) of a class directly without explicitly calling class name.
  • The main difference is Readability, ClassName.dataMember (System.out) is less readable when compared to dataMember(out), static import can make your program more readable


Last Updated : 26 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads