Java Reference
In-Depth Information
Note
ReversedInternetdomainnamesarenotalwaysvalidpackagenames.Oneor
moreofitscomponentnamesmightstartwithadigit(
6.com
),containahyphen(
-
)or
other illegal character (
aq-x.com
), or be one of Java's reserved words (
int.com
).
Conventiondictatesthatyouprefixthedigitwithanunderscore(
com._6
),replacethe
illegal character with an underscore (
com.aq_x
), and suffix the reserved word with
an underscore (
com.int_
).
The Package Statement
Thepackagestatementidentifiesthepackageinwhichasourcefile'stypesarelocated.
This statement consists of reserved word
package
, followed by a member access
operator-separated list of package and subpackage names, followed by a semicolon.
Forexample,
package graphics;
specifiesthatthesourcefile'stypeslocateina
packagenamed
graphics
,and
package graphics.shapes;
specifiesthatthe
source file's types locate in the
graphics
package's
shapes
subpackage.
By convention, a package name is expressed in lowercase. If the name consists of
multiple words, each word except for the first word is capitalized.
Onlyonepackage statement canappear inasourcefile. Whenitispresent, nothing
apart from comments must precede this statement.
Caution
Specifyingmultiplepackagestatementsinasourcefileorplacinganything
apartfromcommentsaboveapackagestatementcausesthecompilertoreportanerror.
Java implementations map package and subpackage names to same-named direct-
ories. For example, an implementation would map
graphics
to a directory named
graphics
, and would map
graphics.shapes
to a
shapes
subdirectory of
graphics
.TheJavacompilerstorestheclassfilesthatimplementthepackage'stypes
in the corresponding directory.
Note
Ifasourcefiledoesnotcontainapackagestatement,thesourcefile'stypesare
saidtobelongtothe
unnamed package
.Thispackagecorrespondstothecurrentdirect-
ory.
The Import Statement
Imagine having to repeatedly specify
ca.tutortutor.graphics.shapes.Circle
or some other lengthy package-
qualified type name for each occurrence of that type in source code. Java provides an