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.
2.0 4.0 6.3
2.0 4.0 6.3
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.
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
- Understanding "static" in "public static void main" in Java
- Static and non static blank final variables in Java
- Difference between static and non-static variables in Java
- Difference between static and non-static method in Java
- static keyword in java
- Static class in Java
- Static blocks in Java
- Initialize a static Map using Java 9 Map.of()
- Initialize a static Map using Stream in Java
- Static method in Interface in Java
- Static Control Flow in Java
- Why can't static methods be abstract in Java?
- Shadowing of static functions in Java
- Initialize a static map in Java with Examples
- Comparison of static keyword in C++ and Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.