Static import in Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

2.0
4.0
6.3
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:



2.0
4.0
6.3
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Error:Reference to MAX_VALUE is ambigious

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 ambigious.

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 Readablity, ClassName.dataMember (System.out) is less readable when compared to dataMember(out), static import can make your program more readable


My Personal Notes arrow_drop_up

Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator I just love Programming languages and love to know new concepts everyday,every minute,every second Here to help Other GEEKS

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.