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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Understanding "static" in "public static void main" in Java
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- Understanding storage of static methods and static variables in Java
- Why non-static variable cannot be referenced from a static method in Java
- Internal static variable vs. External static variable with Examples in C
- Comparison of static keyword in C++ and Java
- Output of Java Programs | Set 48 (Static keyword)
- Static method in Interface in Java
- Static Variables in Java with Examples
- Why can't static methods be abstract in Java?
- Initialize a static map in Java with Examples
- Initialize a static Map using Java 9 Map.of()
- Initialize a static Map in Java using Double Brace Initialization
- Can static functions be virtual in C++?
- Some interesting facts about static member functions in C++
- Static data members in C++
- When are static objects destroyed?
- C++ | Static Keyword | Question 1
- C++ | Static Keyword | Question 2
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.