Java static import

In java static import is the most common and confusing topic. Most of the programmer already know what is import in java and how does it work. But the question arises what static import in java is and what is the need of it. In this post, we will learn about the java static import.

Here is the table content of the article will we will cover this topic.
1. What is the java static import?
2. How to use java static import?
i) import all data members by an asterisk
ii) import static variable by name
iii) import static method by name
3. How to resolve the conflict of the same data member?
4. Important points about static import

What is the java static import?

As we know import in java is used to import the classes, interfaces, and enums. In Java static import introduced in 1.5 version. By the use of java static import, we can access the static variables or static methods of the imported class. We don’t need to use the className or object to access them. Whenever we import a class by use of static import, then all the static variable and static method behave like as they declared in the same class.

Let’s take an example of Math class. Suppose we want to do some operation by use of Math class. Math class has a lot of methods that we can use according to our needs.

public class ExampleImport
{
	public static void main(String[] args) 
	{
		ExampleImport obj = new ExampleImport();
		obj.calculateData(8);
	}
	
	void calculateData(int a)
	{
		double root = Math.sqrt(a);
		System.out.println(Math.round(root));
		System.out.println(2 * Math.PI);
	}
}

Output: 3
6.283185307179586

As you have seen, we are using Math class every time wherever we are using the method to perform the operation. Let’s see how it will work.

import static java.lang.Math.*;
public class ExampleImport
{
	public static void main(String[] args) 
	{
		ExampleImport obj = new ExampleImport();
		obj.calculateData(8);
	}
	
	void calculateData(int a)
	{
		double root = sqrt(a);
		System.out.println(round(root));
		System.out.println(2 * PI);
	}
}

Output: 3
6.283185307179586

By the use of Java static import, we don’t need to use class name every time. We can access the static variable and static method with the class name. In the above example sqrt() and PI are the static member of Math class.

How to use java static import

In java, static import is used to import the static member of the class. We can import static data member by name or by use of asterisk symbol.

1. import all data members by an asterisk

Suppose we want to import all the static data member of class, then we will use the special syntax with an asterisk.

java static import

The static import statements must be written as ‘import static’ in code and you can write it ‘static import’. Suppose you want to use all the static data members of Math class then you should use the asterisk symbol to import all as we have discussed above.

2. import static variable by name

There may be a situation when we want to import only a specific static variable. To import the static variable we will use the specific syntax:

java static import

Suppose we want to use the static variable of Math class in the program. Let’s say we want to perform calculate by use of PI (static variable).

 import static java.lang.Math.PI;
public class ExampleImport
{
	public static void main(String[] args) 
	{
		ExampleImport obj = new ExampleImport();
		obj.calculateData(2);
	}
	
	void calculateData(int a)
	{
		System.out.println(2 * PI);
	}
}

Output: 6.283185307179586

3. import static method by name

Suppose we want to import the static method of the class. Suppose we want to import only a specific static method. To import the static method we will use the specific syntax:

java static import

Suppose we want to use the static method of Math class in the program. Let’s say we want to perform square root of any number. Then we can import the sqrt() method of Math class.

 import static java.lang.Math.sqrt;
public class ExampleImport
{
	public static void main(String[] args) 
	{
		ExampleImport obj = new ExampleImport();
		obj.calculateData(2);
	}
	
	void calculateData(int a)
	{
		System.out.println(2 * sqrt(a));
	}
}

Output: 2.8284271247461903

How to resolve the conflict of the same data member?

In java static import there may be some static variable or static method that can have the same name. In this type of situation when two classes have the same static data member the compiler shows the ambiguity error. we need to resolve the conflicts.

In the below example, We have two classes ClassOne and ClassTwo. Both have the same data members that are data (static variable) and multiplication() method (static method).

java static import

In this case the compiler gets confused which data member should be called? To resolve this conflict, we can use the full qualified name to access the data member of class. We have already discussed in separate post, how to use full qualified name in import ?

package package1;

public class ClassOne 
{
	public static int data = 10;
	public static int multiplication(int b, int c)
	{
		return b*c;
	}
}
package package1;

public class ClassTwo 
{
	public static int data = 5;
	public static int multiplication(int b, int c)
	{
		return b*c;
	}
}
import static package1.ClassOne.*;
import static package1.ClassTwo.*;
public class ExampleImport
{
	public static void main(String[] args) 
	{
		System.out.println(new package1.ClassOne().data);
		
		System.out.println(new package1.ClassOne().multiplication(1, 2));	
	}
}

Output: 10
2

Important points about static import

1. As you know we can import the static members of a class by use of asterisk and with the specific data member name.  If two classes have the same name static members and we are importing them like:

One data member is importing by use of data member name like

import static packageName.dataMember;

and another is importing by use of asterisk.

import static packageName.dataMember;

Suppose we have two classes, package1.ClassOne and package1.ClassTwo. In both classes have a static method called multiplication(int b, int c). We are importing the multiple() method by use of asterisk and by method name.

package package1;

public class ClassOne 
{
	public static int data = 10;
	public static int operation(int b, int c)
	{
		return b*c*data;
	}
}
package package1;

public class ClassTwo 
{
	public static int data = 5;
	public static int operation(int b, int c)
	{
		return b*c*data;
	}
}
import static package1.ClassOne.operation;
import static package1.ClassTwo.*;
public class ExampleImport
{
	public static void main(String[] args) 
	{
		System.out.println(operation(1, 2));
	}
}

Output: 20

As you can see in the above example the JVM using the operation method of ClassOne, because we are importing it on demand.

2. We can’t import two static members with the same name by import static packageName.VariableName Java doesn’t permit it and shows the compilation error. Let’s try to import two variables with the same in a program.

3. Suppose we are importing a static member from another and the same name static member already exists in the class. Then the JVM will give preference to the static member, that presented in the same class.

import static package1.ClassOne.data;

public class ExampleImport
{
	static int data = 5;
	public static void main(String[] args) 
	{
		System.out.println(data);
	}
}

Output: 5

2 thoughts on “Java static import”

  1. Your entire website is very good and provides detailed explanation, but there are a few mistakes which are sometimes confusing. It’ll be great if you can edit them.

    Reply
  2. The simplest explanation I have ever read. Thank you so much for clearing my doubts which occurs regularly 😂

    Reply

Leave a Comment