More Features of the Java Language |
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 eitherEach is appropriate for different situations, as explained in the following sections.
- refer to the member by its long (disambiguated) name,
- import the package member, or
- import the member's entire package.
Referring to a Package Member by Name
So far, the examples in this book have referred to classes and interfaces by the name specified in their declaration (such asRectangle
,AlarmClock
, andSleeper
). uch names are called short names. 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.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 long name, which includes the package name. This is the long name for the
Rectangle
class declared in thegraphics
package in the previous example:You could use this long name to create an instance ofgraphics.Rectanglegraphics.Rectangle
:You'll find that using long names is okay for one-shot uses. But you'd likely get annoyed if you had to writegraphics.Rectangle myRect = new graphics.Rectangle();graphics.Rectangle
again and again. Also, your code would get very 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 animport
statement at the beginning of your file before any class or interface definitions (but after thepackage
statement, if there is one). Here's how you would import theCircle
class from thegraphics
package created in the previous section:Now you can refer to theimport graphics.Circle;Circle
class by its short name:This approach works just fine if you use just a few members from theCircle myCircle = new Circle();graphics
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.Importing an Entire Package
To import all of the classes and interfaces contained in a particular package, use the import statement with the asterisk*
wildcard character:Now you can refer to any class or interface in theimport graphics.*;graphics
package by its short name:The asterisk in theCircle myCircle = new Circle(); Rectangle myRectangle = new Rectangle();import
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 thegraphics
package that begin with "A":Instead, it generates a compiler error. With theimport graphics.A*; // does not workimport
statement, you can import only a single package member or an entire package.For your convenience, the Java runtime system automatically imports three packages for you:
- The default package (the package with no name)
- The
java.lang
package- The current package
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 the member by its long name. For example, the previous example defined a class namedRectangle
in thegraphics
package. Thejava.awt
package also contains aRectangle
class. If bothgraphics
andjava.awt
have been imported, then the following is ambiguous:In such a situation, you have to be more specific and indicate exactly whichRectangle rect;Rectangle
class you want by using the member's long name:graphics.Rectangle rect;
More Features of the Java Language |