Since classes within packages must be fully qualified with their package name or names, it could become tedious to type in the long dot-separated package path name for every class you want to use. For this reason, Java includes the import
statement to bring certain classes, or entire packages, into visibility. Once imported, a class can be referred to directly, using only its name. The import statement is a convenience to the programmer and is not technically needed to write a complete Java program.
In a Java source file, import statements occur immediately following the package statement (if it exists) and before any class definitions. This is the general form of the import statement:
import pkg1[.pkg2].(classname/*);
Here, pkg1 is the name of a top-level package, and pkg2 is the name of a subordinate package inside the outer package separated by a dot (.). There is no practical limit on the depth of a package hierarchy, except that imposed by the file system. Finally, you specify either an explicit classname or a star (*), which indicates that the Java compiler should import the entire package.
import java.util.Date;
import java.io.*;
If a class with the same name exists in two different packages that you import using the star form, the compiler will remain silent, unless you try to use one of the classes. In that case, you will get a compile-time error and have to explicitly name the class specifying its package.
package MyPack;
/* Now, the Balance class, its constructor, and its
show() method are public. This means that they can
be used by non-subclass code outside their package.
*/
public class Balance {
String name;
double bal;
public Balance(String n, double b) {
name = n;
bal = b;
}
public void show() {
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
As you can see, the Balance
class is now public
. Also, its constructor and its show()
method are public, too. This means that they can be accessed by any type of code outside the MyPack
package. For example, here TestBalance
imports MyPack
and is then able to make use of the Balance
class:
import MyPack.*;
class TestBalance {
public static void main(String args[]) {
/*
* Because Balance is public, you may use Balance
* class and call its constructor.
*/
Balance test = new Balance("J. J. Jaspers", 99.88);
test.show(); // you may also call show()
}
}
J. J. Jaspers: $99.88