The types that comprise a package are known as the package members.To use a
public
package member from outside its package, you must do one of the following:Each is appropriate for different situations, as explained in the sections that follow.
- Refer to the member by its fully qualified name
- Import the package member
- Import the member's entire package
Referring to a Package Member by Its Qualified Name
So far, most of the examples in this tutorial have referred to types by their simple names, such asRectangle
andStackOfInts
. You can use a package member's simple name if the code you are writing is in the same package as that member or if that member has been imported.However, if you are trying to use a member from a different package and that package has not been imported, you must use the member's fully qualified name, which includes the package name. Here is the fully qualified name for the
Rectangle
class declared in thegraphics
package in the previous example.You could use this qualified name to create an instance ofgraphics.Rectanglegraphics.Rectangle
:Qualified names are all right for infrequent use. When a name is used repetitively, however, typing the name repeatedly becomes tedious and the code becomes difficult to read. As an alternative, you can import the member or its package and then use its simple name.graphics.Rectangle myRect = new graphics.Rectangle();Importing a Package Member
To import a specific member into the current file, put animport
statement at the beginning of the file before any type definitions but after thepackage
statement, if there is one. Here's how you would import theRectangle
class from thegraphics
package created in the previous section.Now you can refer to theimport graphics.Rectangle;Rectangle
class by its simple name.This approach works well if you use just a few members from theRectangle myRectangle = new Rectangle();graphics
package. But if you use many types from a package, you should import the entire package.Importing an Entire Package
To import all the types contained in a particular package, use theimport
statement with the asterisk(*)
wildcard character.Now you can refer to any class or interface in theimport graphics.*;graphics
package by its simple name.The asterisk in theCircle myCircle = new Circle(); Rectangle myRectangle = new Rectangle();import
statement can be used only to specify all the classes within a package, as shown here. It cannot be used to match a subset of the classes in a package. For example, the following does not match all the classes in thegraphics
package that begin withA
.Instead, it generates a compiler error. With theimport graphics.A*; //does not workimport
statement, you generally import only a single package member or an entire package.For convenience, the Java compiler automatically imports three entire packages for each source file: (1) the package with no name, (2) the
Note: Another, less common form ofimport
allows you to import the public nested classes of an enclosing class. For example, if thegraphics.Rectangle
class contained useful nested classes, such asRectangle.DoubleWide
andRectangle.Square
, you could importRectangle
and its nested classes by using the following two statements.Be aware that the second import statement will not importimport graphics.Rectangle; import graphics.Rectangle.*;Rectangle
.Another less common form of
import
, the static import statement, will be discussed at the end of this section.java.lang
package, and (3) the current package (the package for the current file).Apparent Hierarchies of Packages
At first, packages appear to be hierarchical, but they are not. For example, the Java API includes ajava.awt
package, ajava.awt.color
package, ajava.awt.font
package, and many others that begin withjava.awt
. However, thejava.awt.color
package, thejava.awt.font
package, and otherjava.awt.xxxx
packages are not included in thejava.awt
package. The prefixjava.awt
(the Java Abstract Window Toolkit) is used for a number of related packages to make the relationship evident, but not to show inclusion.Importing
java.awt.*
imports all of the types in thejava.awt
package, but it does not importjava.awt.color
,java.awt.font
, or any otherjava.awt.xxxx
packages. If you plan to use the classes and other types injava.awt.color
as well as those injava.awt
, you must import both packages with all their files:import java.awt.*; import java.awt.color.*;Name Ambiguities
If a member in one package shares its name with a member in another package and both packages are imported, you must refer to each member by its qualified name. For example, thegraphics
package defined a class namedRectangle
. Thejava.awt
package also contains aRectangle
class. If bothgraphics
andjava.awt
have been imported, the following is ambiguous.In such a situation, you have to use the member's fully qualified name to indicate exactly whichRectangle rect;Rectangle
class you want. For example,graphics.Rectangle rect;The Static Import Statement
There are situations where you need frequent access to static final fields (constants) and static methods from one or two classes. Prefixing the name of these classes over and over can result in cluttered code. The static import statement gives you a way to import the constants and static methods that you want to use so that you do not need to prefix the name of their class.The
java.lang.Math
class defines thePI
constant and many static methods, including methods for calculating sines, cosines, tangents, square roots, maxima, minima, exponents, and many more. For example,Ordinarily, to use these objects from another class, you prefix the class name, as follows.public static final double PI 3.141592653589793 public static double cos(double a)You can use the static import statement to import the static members of java.lang.Math so that you don't need to prefix the class name,double r = Math.cos(Math.PI * theta);Math
. The static members ofMath
can be imported either individually:or as a group:import static java.lang.Math.PI;Once they have been imported, the static members can be used without qualification. For example, the previous code snippet would become:import static java.lang.Math.*;Obviously, you can write your own classes that contain constants and static methods that you use frequently, and then use the static import statement. For example,double r = cos(PI * theta);import static mypackage.MyConstants.*;
Note: Use static import very sparingly. Overusing static import can result in code that is difficult to read and maintain, because readers of the code won't know which class defines a particular static object. Used properly, static import makes code more readable by removing class name repetition.