The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Interfaces and Packages

Using Package Members

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 do one or more of the following: Each is appropriate for different situations, as explained in the following sections.

Referring to a Package Member by Name

So far, the examples in this tutorial have referred to types by their simple names, such as Rectangle and StockWatcher. 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 qualified name, which includes the package name. This is the qualified name for the Rectangle class declared in the graphics package in the previous example:

graphics.Rectangle
You could use this long name to create an instance of graphics.Rectangle:
graphics.Rectangle myRect = new graphics.Rectangle();
You'll find that using long names is okay for one-shot uses. But you'd likely get annoyed if you had to write graphics.Rectangle again and again. Also, your code would get messy and difficult to read. In such cases, you can just import the member instead.

Importing a Package Member

To import a specific member into the current file, put an import statement at the beginning of your file before any class or interface definitions but after the package statement, if there is one. Here's how you would import the Circle class from the graphics package created in the previous section:
import graphics.Circle;
Now you can refer to the Circle class by its simple name:
Circle myCircle = new Circle();
This approach works well if you use just a few members from the graphics package. But if you use many types from a package, you can import the entire package.

Importing an Entire Package

To import all the types contained in a particular package, use the import statement with the asterisk (*) wildcard character:
import graphics.*;
Now you can refer to any class or interface in the graphics package by its short name:
Circle myCircle = new Circle();
Rectangle myRectangle = new Rectangle();
The asterisk in the 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
Instead, it generates a compiler error. With the import statement, you generally import only a single package member or an entire package.

Note: Another, less common form of import allows you to import only a class and its public inner classes. For example, if the graphics.Rectangle class contained useful inner classes, like Rectangle.DoubleWide and Rectangle.Square, you could import Rectangle and its inner classes this way:
import graphics.Rectangle.*;
For more information, see the section on Inner Classes (in the Learning the Java Language trail)

For your convenience, the Java compiler automatically imports three entire packages:


Note: Packages aren't hierarchical. For example, importing java.util.* doesn't let you refer to the Pattern class as regex.Pattern. You must always refer to it as either java.util.regex.Pattern or (if you import java.util.regex.*) simply Pattern.

Disambiguating a Name

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 each member by its qualified name. For example, the previous example defined a class named Rectangle in the graphics package. The java.awt package also contains a Rectangle class. If both graphics and java.awt have been imported, the following is ambiguous:
Rectangle rect;
In such a situation, you have to be more specific and use the member's qualified name to indicate exactly which Rectangle class you want:
graphics.Rectangle rect;

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.