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, especially in large codebases or projects with multiple developers.
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 by eliminating repetitive class names and making code more concise.
Similar Reads
Static Blocks in Java In simpler language whenever we use a static keyword and associate it to a block then that block is referred to as a static block. Unlike C++, Java supports a special block, called a static block (also called static clause) that can be used for static initialization of a class. This code inside the
4 min read
Static class in Java Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class
3 min read
Static Control Flow in Java Static Control Flow decides the sequence of activities/steps that will be executed in order when we run a java class that contains static variables, methods, and blocks. This article will explain how static control flow occurs whenever a Java program is executed. Prerequisite: Static Blocks The Stat
4 min read
Important Keywords in Java Keywords refer to the reserved set of words of a language. These are used for some predefined actions. abstract: It is a non-access modifier applicable for classes and methods. It is used to achieve abstraction. For more, refer to abstract keyword in javaenum: It is used to define enum in Javainstan
2 min read
Final Static Variable in Java When the value of a variable is not varied, then it is not a good choice to go for an instance variable. At that time, we can add a static modifier to that variable. Whenever we declare a variable as static, then at the class level, a single variable is created which is shared with the objects. Any
3 min read
Static vs Dynamic Binding in Java There are certain key points that are needed to be remembered before adhering forward where we will be discussing and implementing static and dynamic bindings in Java later concluding out the differences. private, final and static members (methods and variables) use static binding while for virtual
5 min read