Site icon JavaGoal

Java import package

You can access classes, interfaces, enumeration, or sub-packages from any package by using java import package statements. There is no need to import a class if it exists in the same package and we can directly access it. Classes in the same package find each other without any problem. But when class, interface, or enum is in another package then you have to use the import statement and java static import to use the packages.

By using of import statement in java, you can use or access any class, interface, and packages in your class. In this post, we will learn how to java import package.

There are some ways to import the packages: 
1. import all classes and interface
2. import particular class
3. fully qualified name
4. static import in java

1. import all classes and interfaces

Suppose you want to import all the classes, interfaces, or enums from the package. You can access all the classes, interfaces, or enums which reside in the package. You can access them by
use an asterisk(*) symbol.
NOTE: Sub-package will not be accessible by this statement.
Here is the syntax to import all the classes and interfaces:

import packageName.*;

import is a special keyword that is used to import other classes and interfaces in the current class.
packageName is the name of the package from which you want to access the classes, interfaces or enums.
dot(.) defines the directory structure of packages.
asterisk(*) is used to import all the classes, interfaces, or enums.

import java.util.*;

In the above example, util is a package, and an asterisk(*) is used to import all the classes, interfaces, or enums from the util package.

package myPackage;
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

It is a very common scenario when you don’t want to import all classes of packages. You just want to import a single class. You can import a particular class by use of syntax

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

import packageName.className;

import is a special keyword that is used to import other classes and interfaces in the current class.
packageName is the name of the package from which you want to access the classes, interfaces or enums.
dot(.) defines the directory structure of packages.
className is the name of the class that you want to import.

import java.util.Date;

In the above example, util is a package, and the Date is a class that we want to import.

package myPackage;
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. fully qualified name

We can import any class by use of a fully qualified name. By means of the fully qualified name is className with packageName. There are two most important scenarios when you need to import the class with a fully qualified name.

Scenario 1

If you are importing a class by use of a fully qualified name, then it will be accessible only in the declared class. It will be accessible only where it is declared. There is no need to import. But you need to use a fully qualified name every time when you are accessing the class or interface.

packageName.className;

packageName is the name of the package from which you want to access the classes, interfaces or enums.
dot(.) defines the directory structure of packages.
className is the name of the class that you want to import.

java.util.date;

In the above example, util is a package, and the Date is a class that we want to import.

package myPackage;

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

Scenario 2

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 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 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 do 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

4. static import statement 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 static import 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

Exit mobile version