import in java

The most common keyword used in java programs is the import keyword. The import in java is directly connected to the package in java. If you are a beginner and you don’t know about the package in java and access of the package, then we would recommend you please read the package in java first. The import in java is used to import the package, sub-package, class, interface, and enum in the program. In this post, we will learn how we can import in java.

The concept of import in java came from the concept of the package in java. As we know packages provide an organized structure to our projects. We can define related classes or interfaces in similar types of packages. But we need to use the import statement in the program to use them.

Suppose we are working on a big project and managing the classes or interfaces through the packages. We can keep the utility class or common functionality in separate packages so that whenever we need it, we can you. To use that utility class, you will tell the compiler by use of the import statement.

import in java

There are some ways to import the packages: 
1. import all classes, interfaces, enum by use of the asterisk
2. import particular class by use of className
3. import in java by fully qualified className
4. How to resolve conflict during import package

5. What is the java static import?
6. How to use java static import?
7. How to resolve the conflict of the same data member?
8. Important points about static import?

1. import all classes, interfaces, and enum by use of the asterisk

If we use the asterisk with packageName like packagename.*, then all the classes, interfaces, and enums automatically import in java program. But in some conditions, it’s not a good practice. Suppose you need only one class from the package but by use of an asterisk(*), it will import all the classes, interface, and enum that are not useful and make a burden on your java program. We can import a single class also which will discuss in the below section.

import packageName.*;
import in java
import java.util.*;

NOTE: Here all the classes, interfaces, and enums are imported from the package. But the classes, interfaces, and enums inside the sub-packages don’t be available for use.

Let’s take the example of java.util package. It is one of the most common packages used in programs. Here we are importing all the classes, interfaces, and enums of java.util package.

import java.util.*;
public class MyClass 
{
	public static void main(String[] args)
	{
		System.out.println("Here we are using classes of util package");
		System.out.println(new Date());
	}
}

Output: Here we are using classes of util package
Sun Oct 20 22:37:43 IST 2019

2. import particular class

If we use the className with packageName like packagename.className, we can import only one class. Sometimes we just need to import only a single class from the package. For example, if we want to print the current date of the system then we will use the Date class that exists in the util package. So here we don’t need to import all classes by an asterisk. We should import only a particular class from the util package.

import packageName.className;
import in java
import java.util.Date;

NOTE: It is a good practice to import a particular class if you want to access only some classes. If you have any package that has thousands of classes, then you should import the classes that will be in use.

import java.util.Date;
public class MyClass 
{
	public static void main(String[] args)
	{
		System.out.println("Here we are using Date class of util package");
		System.out.println(new Date());
	}
}

Output:
Here we are using Date class of util package
Sun Oct 20 22:36:41 IST 2019

3. import in java by fully qualified className

You can also import a class by use of a fully qualified name. If you are accessing a class by use of a fully qualified name, then it will be accessible only in a declared class. Now there is no need to import. But you need to use a fully qualified name every time wherever you want to access it. You can access any class by use of the fully qualified name.

packageName.className;
import in java
java.util.date;
public class ITDepartment {

	public static void main(String[] args)  
	{
		System.out.println("Here we are using fully qualified name of class");
		System.out.println(new java.util.Date());
	}
}

Output: Here we are using fully qualified name of class
Sun Oct 20 22:43:33 IST 2019

How to resolve conflict during import package

There may be a situation when class name conflict may occur. Let’s say we have two packages java.util and java.sql Both the packages have a class with the same name which is the Date class. Now suppose a class imports both these packages like this.

import java.util.Date;
import java.sql.Date;
public class ExampleOfPackage 
{
    
    public static void main(String arg[])
    {
        System.out.println("Date with time : "+ new Date(0));
        System.out.println("Sql date : "+ new Date(0));
    }
}

It shows an error at compile time “The import java.sql.Date collides with another import statement

In this type of scenario, we should use the full qualified name of the class with the package. If we will not use a fully qualified name then the compiler will get confused about which class should be used.

import java.util.Date;
public class ExampleOfPackage 
{
    
    public static void main(String arg[])
    {
        System.out.println("Date with time : "+ new Date(0));
        System.out.println("Sql date : "+ new java.sql. Date(0));
    }
}

Output: Date with time : Thu Jan 01 05:30:00 IST 1970
Sql date : 1970-01-01

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 is introduced in the 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 variables and static methods 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 use Math class every time wherever we use 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 names 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 the 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 an asterisk symbol.

1. import all data members by an asterisk

Suppose we want to import all the static data members of the 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 as ‘static import’. Suppose you want to use all the static data members of the 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 the Math class in the program. Let’s say we want to perform calculations 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 the 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?

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 which are the data (static variable) and multiplication() method (static method).

java static import

In this case, the compiler gets confused about which data member should be called. To resolve this conflict, we can use the full qualified name to access the data member of the class. We have already discussed in a separate post, how to use a fully 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 an 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 imported by use of a data member name like

import static packageName.dataMember;

and another is importing by use of an 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 an 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 uses 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

1 thought on “import in java”

Leave a Comment