







<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--NewPage-->
<html>
<head>
<title>Using Package Members</title>
</head>
<body BGCOLOR="#ffffff">
<table width="100%">
<tr>
<td align=left>
<a href="createpkgs.html"><img src="../../images/PreviousArrow.gif" width=26 height=26 align=bottom border=0 alt="Previous | "></a><a
href="managingfiles.html"><img src="../../images/NextArrow.gif" width=26 height=26 align=bottom border=0 alt="Next | "></a><a
href="../../trailmap.html"><img src="../../images/WayUpArrow.gif" width=26 height=26 align=bottom border=0 alt="Trail Map | "></a><a
href="../index.html"><img src="../../images/javaHeader.gif" width=26 height=26 align=bottom border=0 alt="Learning the Java Language | "></a>
</td>
<td align=right>
<a href="index.html"><strong><em>More Features of the Java Language</em></strong></a>
</td>
</tr>
</table>
<p>
<hr size=4>

<h2>
    Using Package Members
</h2>
<p>
<blockquote>

Only public package members are accessible outside the package in which
they are defined. To use a public package member from outside its
package, you must either
<ul>
<li>
refer to the member by its long (disambiguated) name,
<li>
import the package member, or
<li>
import the member's entire package.
</ul>
Each is appropriate for different situations, as explained in the
following sections.

<h4>Referring to a Package Member by Name</h4>
<blockquote>
So far, the examples in this book have referred to classes and
interfaces by the name specified in their declaration (such as
<code>Rectangle</code>, <code>AlarmClock</code>, and <code>Sleeper</code>).
uch names are called <em>short names</em>.
You can use a package member's short name if the code you are writing
is in the same package as that member or if the member's package has
been imported.
<p>
However, if you are trying to use a member from a different package and
that package has not been imported, then you must use the member's <em>long
name</em>, which includes the package name. This is the long name for the
<code>Rectangle</code> class declared in the <code>graphics</code>
package in the previous example:
<blockquote>
<pre>
graphics.Rectangle
</pre>
</blockquote>
You could use this long name to create an instance of
<code>graphics.Rectangle</code>:
<blockquote>
<pre>
graphics.Rectangle myRect = new graphics.Rectangle();
</pre>
</blockquote>
You'll find that using long names is okay for one-shot uses. But you'd
likely get annoyed if you had to write <code>graphics.Rectangle</code>
again and again. Also, your code would get very messy and difficult to read.
In such cases, you can just import the member instead.
</blockquote>

<h4>Importing a Package Member</h4>
<blockquote>
To import a specific member into the current file, put an <code>import</code>
statement at the beginning of your file before any class or interface
definitions (but after the <code>package</code> statement, if there is one).
Here's how you would import the <code>Circle</code> class from the
<code>graphics</code> package created in the previous section:
<blockquote>
<pre>
import graphics.Circle;
</pre>
</blockquote>
Now you can refer to the <code>Circle</code> class by its short name:
<blockquote>
<pre>
Circle myCircle = new Circle();
</pre>
</blockquote>
This approach works just fine if you use just a few members from the
<code>graphics</code> package.
But if you use many classes and interfaces from a
package, you really just want to import the whole package and forget
about it.
</blockquote>

<h4>Importing an Entire Package</h4>
<blockquote>
To import all of the classes and interfaces contained in a particular
package, use the import statement with the asterisk <code>*</code> wildcard
character:
<blockquote>
<pre>
import graphics.*;
</pre>
</blockquote>
Now you can refer to any class or interface
in the <code>graphics</code> package by
its short name:
<blockquote>
<pre>
Circle myCircle = new Circle();
Rectangle myRectangle = new Rectangle();
</pre>
</blockquote>
The asterisk in the <code>import</code> statement
can be used only to specify all of
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 of the classes
in the <code>graphics</code> package that begin with "A":
<blockquote>
<pre>
import graphics.A*;	// <em>does not work</em>
</pre>
</blockquote>
Instead, it generates a compiler error. With the <code>import</code>
statement, you
can import only a single package member or an entire package.
<p>
For your convenience, the Java runtime system automatically imports
three packages for you:
<ul>
<li>
The default package (the package with no name)
<li>
The <code>java.lang</code> package
<li>
The current package
</ul>
</blockquote>

<h4>Disambiguating a Name</h4>
<blockquote>
If by some chance a member in one package shares the same name with a
member in another package and both packages are imported, you must
refer to the member by its long name. For example, the previous example
defined a class named <code>Rectangle</code> in the <code>graphics</code>
package. The <code>java.awt</code>
package also contains a <code>Rectangle</code> class.
If both <code>graphics</code> and <code>java.awt</code>
have been imported, then the following is ambiguous:
<blockquote>
<pre>
Rectangle rect;
</pre>
</blockquote>
In such a situation, you have to be more specific and indicate exactly
which <code>Rectangle</code> class you want by using the member's long name:
<blockquote>
<pre>
graphics.Rectangle rect;
</pre>
</blockquote>
</blockquote>

</blockquote>
<p>
<hr size=4>
<p>
<table width="100%">
<tr>
<td align=left>
<a href="createpkgs.html"><img src="../../images/PreviousArrow.gif" width=26 height=26 align=top border=0 alt="Previous | "></a><a
href="managingfiles.html"><img src="../../images/NextArrow.gif" width=26 height=26 align=top border=0 alt="Next | "></a><a
href="../../trailmap.html"><img src="../../images/WayUpArrow.gif" width=26 height=26 align=top border=0 alt="Trail Map | "></a><a
href="../index.html"><img src="../../images/javaHeader.gif" width=26 height=26 align=top border=0 alt="Learning the Java Language | "></a>
</td>
<td align=right>
<a href="index.html"><strong><em>More Features of the Java Language</em></strong></a>
</td>
</tr>
</table>
</body>
</html>
