Example of Static import in Java
Static keyword
Java's static keyword is mainly used to control memory. Variables, methods, blocks, and nested classes are compatible with the static keyword. Instead of an instance, the static keyword is part of the class.
The static may be
- Variable (also can be said as inference variable or class variable)
- Method (also can be expressed as or class Method)
- Block
- nested class
Static as import statement in Java
The concept of static import was first introduced in Java 1.5. A static import lets us immediately access a class's static members without using the class name or other objects. For instance, even though we typically access the Math class' sqrt() method through Math. sqrt(), we can also access it directly using a static import.
SUN MicroSystem claims that it will boost coding and make code more readable. However, specialists in programming claim that it will cause confusion and be bad for programming. We shouldn't use static import if there are no precise requirements.
The Java 5 static import feature makes it easier for programmers to readily access every static member of such a class. It doesn't need to be qualified with the class name.
Advantages of using static import statements in Java
Using a static import statement requires minimal coding as you can access the static member in that class any number of times, as you can directly call the static members.
The disadvantage of using static import statements in Java
Using this static import statement often in the program makes the code complex as it would be unreadable and hard to understand for others.
StaticExp.java
import static Java. lang.System.*;
class StaticExp {
public static void main (String args []) {
int a = 10, b = 20; // Initializing the variables
out.println("the sum of a + b :" + “ “ + a+b);
// You need not use System. out every time as it is a static member
out.println(“sum of a + 10 :”+ “ “ + a + 10);
}
}
Output:
the sum of a + b : 30
sum of a + 10 : 20
You can use this static import for any method.
StaticExp1.java
import static Java. lang.Math.*;
// Importing math class as the static member
class StaticExp1 {
public static void main (String [] args)
{
System.out.println(“square root of 25 :” + “ “ + sqrt (25));
// Printing square root of 25 using static class
System.out.println(“power of 3 raised to 3 :” + “ “ + pow (3, 3));
// Printing the power of 3 raised to 3 using static class
System.out.println(“abs of 20.5 :” + “ “ + abs (20.5));
// Printing the abs of 20.5 using static class
}
}
Ambiguity in static import statement:
The compiler will produce an error if two static elements with the same name are loaded from distinct classes since it won't know which element to use in the missing class identity qualification.
StaticExp2.java
import static Java. lang.Integer.*;
import static Java. lang.Byte.*;
class StaticExp2 {
public static void main(String[] args)
{
out.println(MIN_VALUE);
}
}
Output
Error : Reference to MIN_VALUE is ambiguous
We are attempting to access the MIN VALUE variable in the programme above; however, every primitive data type already has a MIN VALUE variable pre-declared in its Wrapper class. We are importing the Integer and Byte classes in this case and trying to access the static value MIN VALUE. Still, the compiler will become confused by seeing two import instructions because the static variable MIN VALUE is present in both the Integer and Byte classes. As a result, the compiler reported that the reference to MIN VALUE was ambiguous.
Difference between import and static import
You can use classes and interfaces that are available in any package by using import. However, by using static import, we may easily access all of a class's static elements (variables or methods) without using the class name.
The critical distinction is readability; compared to data member(out), ClassName.dataMember (System.out) is much less readable. Static import can improve readability in your software.
While the static import feature gives access to the static members of a class even without class qualification, import allows Java programmers to access classes of a module without package qualification. While a static import just makes a class's static members accessible, an import also makes classes and their interfaces accessible.