i

JAVA Complete Course

Importing packages

As mentioned earlier, packages are just folders for Java classes, interfaces and so on. These packages help us to reserve the class namespace and create the code which will be easily maintainable. For instance, we can find two Date classes in Java. However, the recommendation in Java programming is that, only one unique class name is allowed in a Java project.

How did they manage to put two classes with the identical name in JDK?

This was doable because these two Date classes belong to different packages:

  • java.util.Date – this is an usual Date class that can be used anywhere.

  • java.sql.Date – this is a SQL Date which is used only in SQL query and such.

Built-in packages such as: java.lang, java.util, java.io etc. are  java packages that come along with the JDK.

In order to import entire package in our Java file, we can use import statement with asterisk sign (as in earlier examples), or use only certain classes and interfaces defined in the package.

The general form of import statement is:

The import statement is optional in Java. If we want to use class/interface from a certain package, we can also use it with fully qualified name, which includes its full package hierarchy. Below is an example to import package with import statement.