|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Onlypublicpackage members are accessible outside the package in which they are defined. To use apublicpackage member from outside its package, you must do one or more of the following:Each is appropriate for different situations, as explained in the sections that follow.
- Refer to the member by its long (qualified) name
- Import the package member
- Import the member's entire package
So far, the examples in this tutorial have referred to types by their simple names, such asRectangleandStockWatcher. 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. Here is the qualified name for the
Rectangleclass declared in thegraphicspackage 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 all right for one-shot uses; however, you'd probably get annoyed if you had to writegraphics.Rectangle myRect = new graphics.Rectangle();graphics.Rectangleagain and again. Also, the code would get messy and difficult to read. In such cases, you can import the member instead.
To import a specific member into the current file, put animportstatement at the beginning of the file before any class or interface definitions but after thepackagestatement, if there is one. Here's how you would import theCircleclass from thegraphicspackage created in the previous section.Now you can refer to theimport graphics.Circle;Circleclass by its simple name.This approach works well if you use just a few members from theCircle myCircle = new Circle();graphicspackage. But, if you use many types from a package, you can import the entire package.
To import all the types contained in a particular package, use theimportstatement with the asterisk(*)wildcard character.Now you can refer to any class or interface in theimport graphics.*;graphicspackage by its short name.The asterisk in theCircle myCircle = new Circle(); Rectangle myRectangle = new Rectangle();importstatement 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 thegraphicspackage that begin withA.Instead, it generates a compiler error. With theimport graphics.A*; //does not workimportstatement, you generally import only a single package member or an entire package.For convenience, the Java compiler automatically imports three entire packages: (1) the default package (the package with no name), (2) the
Note: Another, less common form ofimportallows you to import only a class and its public inner classes. For example, if thegraphics.Rectangleclass contained useful inner classes, such asRectangle.DoubleWideandRectangle.Square, you could importRectangleand its inner classes by using the following.For more information, see the Inner Classesimport graphics.Rectangle.*;section.
java.langpackage, and (3) the current package by default.
Note: Packages aren't hierarchical. For example, importingjava.util.*doesn't allow you to refer to thePatternclass asregex.Pattern. You must always refer to it as eitherjava.util.regex.Patternor (if you importjava.util.regex.*) simplyPattern.
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 namedRectanglein thegraphicspackage. Thejava.awtpackage also contains aRectangleclass. If bothgraphicsandjava.awthave been imported, the following is ambiguous.In such a situation, you have to be more specific and use the member's qualified name to indicate exactly whichRectangle rect;Rectangleclass you want.graphics.Rectangle rect;
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.