What is importing?
Importing allows us to specify a fully qualified class or interface name only once at the beginning of the .java
file, before the class or interface declaration. The format of an import statement is as follows:
import <package>.<class or interface name>;
For example, look at the following:
import com.packt.javapath.ch04demo.MyApplication;
From now on, this class can be referred to in the code by its name, MyApplication
, only. It is also possible to import all the classes or interfaces of a package using the wildcard character (*
):
import com.packt.javapath.ch04demo.*;
Notice that the preceding import statement imports classes and interfaces of the subpackages of the com.packt.javapath.ch04demo
package. If needed, each subpackage has to be imported separately.
But, before we continue, let's talk about the .java
file structure and packages.
Structure of the .java file and packages
As you already know, the package name reflects the directory structure, starting with the...