Java Reference
In-Depth Information
nestedtypesthatdescribeaclass'sinterfacemustbedeclared public tobeaccessible
from beyond the package.
Note Throughout this topic, I typically don't declare top-level types and their ac-
cessible members public , unless I'm creating a package.
Everypackagehasaname,whichmustbeanonreservedidentifier.Thememberac-
cessoperatorseparatesapackagenamefromasubpackagename,andseparatesapack-
ageorsubpackagenamefromatypename.Forexample,thetwomemberaccessoper-
ators in graphics.shapes.Circle separate package name graphics from the
shapes subpackagename,andseparatesubpackagename shapes fromthe Circle
type name.
Note The standard class library organizes its many classes and other top-level
typesintomultiplepackages.Manyofthesepackagesaresubpackagesofthestandard
java package.Examplesinclude java.io (typesrelatedtoinput/outputoperations),
java.lang (language-orientedtypes), java.lang.reflect (reflection-oriented
language types), java.net (network-oriented types), and java.util (utility
types).
Package Names Must Be Unique
Suppose you have two different graphics.shapes packages, and suppose that
each shapes subpackage contains a Circle class with a different interface. When
the compiler encounters System.out.println(new Circle(10.0, 20.0,
30.0).area()); inthesourcecode,itneedstoverifythatthe area() methodex-
ists.
Thecompilerwillsearchallaccessiblepackagesuntilitfindsa graphics.shapes
package that contains a Circle class. If the found package contains the appropriate
Circle classwithan area() method,everythingisfine;otherwise,ifthe Circle
class does not have an area() method, the compiler will report an error.
This scenario illustrates the importance of choosing unique package names. Spe-
cifically, the top-level package name must be unique. The convention in choosing
this name is to take your Internet domain name and reverse it. For example, I would
choose ca.tutortutor asmytop-level package name because tutortutor.ca
is
my
domain
name.
I
would
then
specify
ca.tutortutor.graphics.shapes.Circle to access Circle .
Search WWH ::




Custom Search