To use a public package member from
outside its package, you must do one of the following:
Rectangle
and StackOfInts. 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 the graphics package in the previous example.
graphics.Rectangle
graphics.Rectangle:
graphics.Rectangle myRect = new graphics.Rectangle();
import statement at the beginning of the file before
any type definitions but after the package
statement, if there is one. Here's how you would import the
Rectangle
class from the graphics package created in the previous
section.
import graphics.Rectangle;
Rectangle class by its simple name.
Rectangle myRectangle = new Rectangle();
graphics package. But if you use many types
from a package, you should import the entire package.
import statement with the asterisk
(*) wildcard character.
import graphics.*;
graphics package by its simple name.
Circle 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
the graphics package that begin with A.
import graphics.A*; //does not work
import
statement, you generally import only a single package member or an
entire package.
import allows you to import
the public nested classes of an enclosing
class. For example, if the
graphics.Rectangle class contained useful nested classes, such as
Rectangle.DoubleWide and Rectangle.Square,
you could import Rectangle and its nested classes by
using the following two statements.
import 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).
java.awt package, a java.awt.color package, a java.awt.font package,
and many others that begin with java.awt. However, the java.awt.color package,
the java.awt.font package, and other java.awt.xxxx packages are not
included in the java.awt package. The prefix java.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 the java.awt package, but it does not import
java.awt.color, java.awt.font, or any other java.awt.xxxx packages. If you plan to use the
classes and other types in java.awt.color as well as those in java.awt, you must import both packages
with all their files:
import java.awt.*; import java.awt.color.*;
graphics package defined a class named Rectangle.
The java.awt
package also contains a Rectangle class. If both
graphics and java.awt have been imported,
the following is ambiguous.
Rectangle rect;
Rectangle
class you want. For example,
graphics.Rectangle rect;
The java.lang.Math class defines the
PI constant and many static methods, including
methods for calculating sines, cosines, tangents, square roots,
maxima, minima, exponents, and many more. For example,
public static final double PI 3.141592653589793 public static double cos(double a)
double r = Math.cos(Math.PI * theta);
Math.
The static members of Math can be imported either individually:
import static java.lang.Math.PI;
import static java.lang.Math.*;
double r = cos(PI * theta);
import static mypackage.MyConstants.*;