Static Import in java 5
static import is a java language feature introduced in version 5 alias code name as tiger
version.
It was introduced to simplify the access of static variables and static members without using class names.
Before static import
is introduced, we have normal import statements for every java class to use the methods and classes with using the class name.
Here is a normal import for List class example
import java.util.List
How the java code looks like before the static import feature is introduced?
Normally in java coding, if we want to use any static
member variables or a method of a class in our code,
we have to first import that class using the normal import
keyword, after that, we have to use variables/methods with class name in the code (for example Math.E)).
Below is the example for normal import usage with accessing the member variables using a qualified class name.
import java.lang.\*;
public class ImportDemo {
public static void main(String args\[\]) {
System.out.println("This is sample program without using static import functionality" + Math.E);
}
}
In the above program,
- we have used
System
andMath
classes from the java.lang package. - In the java program, we don’t need to import any
java.lang
package as the default package imported is thejava.lang
package for every program. - imported
java.lang
package for this tutorial purpose
static import example
Now we will see the example for the static import feature.
We can use the static member variables of java.lang.System
and java.lang.Math
classes without a qualified class name.
import static java.lang.System.\*;
import static java.lang.Math.\*;
public class ImportDemo {
public static void main(String args\[\]) {
out.println("This is sample program without using static import functionality" + E);
}
}
out
is static
field in java.lang.System
class and
E
is static member variable for java.lang.Math
class
For using static member variable E
in Math class in java.lang
Package, we have coded by adding static import something like import static java.lang.Math.*; or java.lang.Math.E.
It is a good idea to define for each static member(Math.E) instead of all static members(Math.*) in the static import declaration.
Static import Advantages
It allows us to call the static members without using the class name in our code.
By making static import code statements make all the static members of that class are available to declare java class.
Static import can be used for static member variables as well as static members without referring to the class name in java code.
It simplifies the code readability for the constants.
Static import Disadvantages:-**
if we are importing the two same static constants from different classes in the same class. there is the possibility of ambiguity for the compiler to throw an error.
What is the difference between Static import and Normal Import
The basic difference between normal import
and static import
is normal import can call the member variables using the class name in the package.static import
can be defined and static member fields, can not refer to any class name, and used directly without a class name.
Static Import | Normal Import |
---|---|
Readability is good | clean code |
No performance improvements | No performance improvements |
Only static members applied | Applied to all types |
Conclusion
To Sum up, Learned Static import feature in java 1.5 version, this new syntax allows the developer to improve readability and clean code.
And also documented advantages and disadvantages, compare this with normal import example.