Static Import feature tutorials in java with examples

Static Import in java 5:-

static import is java language feature introduced in java version 5 alias code name as tiger version. This was introduced to simplify the access of static variables and static members without using classname.Before static import is introduced, we have normal import statetements for every java class to use the methods and classes with using class name

How the java code looks like before static import feature is introduced?

Normally in java coding, if we want to use the any static member variables or a method of a class in our code,
we have to first import that class using normal import keyword, after that we have to use variables/methods with class name in the code (Math.E)). below is the example for normal import usage with access the member variables using 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 and Math classes are used from java.lang package. In java program, we don’t need to import any java.lang package as default package imported is java.lang package for every program. i have written in the above program and imported java.lang package for our this tutorial purpose

The basic difference between normal import and static import is normal import can call the member variables with class name in the package.
static import can be defined and static member fields can not refere any class name and use directly without classname.

static import usage example:-

Now we will see the example for static import feature which we can use the static member variables of java.lang.System and java.lang.Math classes without 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 will always good idea to define for each static member(Math.E) instead of all static members(Math.\*) in static import declaration  
**Advantages of Static import feature:-**  
To call the static members without using class name in our code. By making static import code statments makes all the static members of that class are avilable to declared java class  
Static import can be used for static member variables as well as static members without refering class name in java code. It simplifies the code readablity for the constants.  
**Disadvantages of Static import feature:-**  
if we are importing the two same static constansts from different classes in the same class. there is possiblity of ambiguity for the compilier to throw error.  
Please let us know your comments on this.
Similar Posts