import in java

The most common keyword used in java programs is the import keyword. The import in java is directly connected to 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, 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 the 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 one a big project and managing your 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. static import in java

5. How to resolve conflict during import package

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

If we use the asterisk with packageName like packagename.*, then all the classes, interfaces, 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 that 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 an example of java.util package. It is one of the most common package that 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 the only single class from the package. For example, we want to print the current date of the system then we will use 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 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 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

In above example we are using fully qualified name to print the date. So there is no need to import the class.

4. static import in java

By use of Static import java allows you to access the static member of a class directly without using the fully qualified name. You can read it in detail with different scenarios.

Example without static import:

public class StaticImport 
{
	public static void main(String args[])
	{
	      double squareRootOfNumber = Math.sqrt(8);
	      System.out.println("Square of 5 is:"+ squareRootOfNumber);
	}
}

Output: Square of 5 is:2.8284271247461903

import static java.lang.Math.*;
public class StaticImport 
{
	public static void main(String args[])
	{
	      double squareRootOfNumber = sqrt(8);
	      System.out.println("Square of 5 is:"+ squareRootOfNumber);
	}
}

Output: Square of 5 is:2.8284271247461903

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 that is Date class. Now suppose a class import 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 showing error at compile time “The import java.sql.Date collides with another import statement

In these type of scenario we should use the full qualified name of class with package. If we will not use fully qualified name then compiler will get confused 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

1 thought on “import in java”

Leave a Comment