Java static Import: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-04 03:55:53   7381  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

static import

static import is a feature that expands the capabilities of import keyword. The static import declaration is mimilar to that of import. The static import feature of Java J2SE 5.0 facilitate the java programmer to access any static member of a class directly. This feature eliminates the need of qualifying a static member with the class name. We all know that static member are referred in association with its class name outside the class. Using static import, it is possible to refer to the static member directly without its class name. There are two general form of static import statement.

Advantage of static import

Less coding is required if you have access any static member of a class oftenly. we can use the import statement to import classes from packages and use them without qualifying the package. similarly we can use static import members from classes and use them without qualifying the class name.

Disadvantage of static import

If you over use the static import feature, it makes the program unreadable and unmaintainable.

The first form of static import statement, import only a single static member of a class

Syntax

import static package.class-name.static-member-name;

Example

import static java.lang.Math.sqrt;   //importing static method sqrt of Math class

The second form of static import statement,imports all the static member of a class

Syntax

import static package.class-type-name.*;

Example

import static java.lang.Math.*;	  //importing all static member of Math class

Syntax

import static packageName. SubpackageNmae. ClassName. StaticMemberName;

or

import static packageName. SubpackageNmae. ClassName. *;

Example

double area_of_circle = Math.PI * radius * radius ;

In the above example PI is a static member of the class, Math. So the static member PI is used in the above program with the qualified class name Math Example without using static import

public class Example
{
    public static void main(String[] args)
    {
        System.out.println(Math.sqrt(144));
    }
}

Output:

12
Example using static import
import static java.lang.Math.*;
public class Test
{
    public static void main(String[] args)
    {
        System.out.println(sqrt(144));
    }
}

Output:

12

How to implement static import feature

If we use the static member in an interface and we need to use it in class. Consider the following code that provides an example of importing the interface.

public interface Salary_increment{

public static final double Manager = 0.4;
public static final double Clerk = 0.25;

}

Here let us assume that the interface is available in the subpackage employee_details of the employee package. If we need to accessthe iterface, we can import the interface using the static import statement as follows:

import static employee.employee_details,Salary_increment;
class Salary_hike{

public static void main(String args[])
 {
 double manager_salary = Manager*Manager_current_salary;
 double clerk_salary = Clerk*Clerk_current_salary;
 ....................
 ......
 ....................
 
 }

}


Example without using static import

import static java.lang.System.*;    
class StaticImportExample{  
  public static void main(String args[]){  
     
   out.println("Wellcome to");//Now no need of System.out  
   out.println("atnyla");  
  
 }   
}  

Output:

12