What is static import? What are advantages and disadvantages of static import?

By static importing a class, all static members in that class can be directly used. This avoids using class name for each and every static member. Also, note that even pre defined classes like System,etc… can be static imported.

static import was introduced in Java 1.5 version
Below is an example of static import. As shown in below example, Test is a class in com.p1 package, and Test class has few static members.

package com.p1;

public class Test {
	public static float PI = 3.14f;
	
	public static double getDiameter(double radius)
	{
		return radius/2;
	}
	
	public static double getCircumference(double radius)
	{
		return 2*PI*radius;
	}
	
	public static double getSurfaceArea(double radius)
	{
		return PI*Math.pow(radius, 2);
	}
	
}

In below code snippet, since com.p1.Test.* is imported using static, it is possible to use static members of com.p1.Test class directly, without using class name.

package com.p2;

import static com.p1.Test.*;

public class SIDemo {
	public static void main(String args[])
	{
	double diameter = getDiameter(5.68);
		
	System.out.println("Diameter is: "+diameter);
		
	double s_area = getSurfaceArea(3.86);
		
	System.out.println("Surface Area is: "+s_area);
		
	System.out.println("PI value considered is:"+PI);
	}
}

Can we use multiple static import

Yes, we can use multiple static import statement in a java file. How ever, ensure that there is no ambiguity in the static members of static imported classes.

package com.p1;

public class Test {
	public static float PI = 3.14f;
	
	public static double getDiameter(double radius)
	{
	return radius/2;
	}
	
	public static double getCircumference(double radius)
	{
	return 2*PI*radius;
	}
	
	public static double getSurfaceArea(double radius)
	{
	return PI*Math.pow(radius, 2);
	}
	
	public static void display()
	{
	System.out.println("display() in com.p1.Test");
	}
}

package com.p3;

public class Abc {
	public static void display()
	{
	System.out.println("display() in com.p3.Abc");
	}
}

package com.p2;

import static com.p1.Test.*;
import static com.p3.Abc.*;

public class SIDemo {
	public static void main(String args[])
	{
	double diameter = getDiameter(5.68);
		
	System.out.println("Diameter is: "+diameter);
		
	double s_area = getSurfaceArea(3.86);
		
	System.out.println("Surface Area is: "+s_area);
		
	System.out.println("PI value considered is:"+PI);
		
	//below method call causes compiler error, as display() method
	//is available in both the classes, com.p1.Test and com.p3.Abc
	//display();
	}
}

Disadvantages of static import

Below are disadvantages of static import
1.This may arise readability issues.
2.It may also cause ambiguity, when same member is declared in more than one static imported classes.

You may also like to read:
What is use of static members
Can we have more than one static block
Role of static method in abstract class
how to use packages in Java