Java Reference
In-Depth Information
Fully Qualified Names
The package name is part of the fully qualified type name , so the name of class List is
actually com.deitel.datastructures.List . You can use this fully qualified name in your
programs, or you can import the class and use its simple name (the class name by itself—
List ) in the program. If another package also contains a List class, the fully qualified class
names can be used to distinguish between the classes in the program and prevent a name
conflict (also called a name collision ).
Step 3: Compiling Packaged Types
Step 3 is to compile the class so that it's stored in the appropriate package. When a Java
file containing a package declaration is compiled, the resulting class file is placed in a di-
rectory specified by the declaration. Classes in the package com.deitel.datastructures
are placed in the directory
com
deitel
datastructures
The names in the package declaration specify the exact location of the package's classes.
The javac command-line option -d causes the compiler to create the directories
based on the package declaration. The option also specifies where the top-level directory
in the package name should be placed on your system—you may specify a relative or com-
plete path to this location. For example, the command
javac -d . List.java EmptyListException.java
specifies that the first directory in our package name ( com ) should be placed in the current
directory. The period ( . ) after -d in the preceding command represents the current direc-
tory on the Windows, UNIX, Linux and Mac OS X operating systems (and several others
as well). Similarly, the command
javac -d .. List.java EmptyListException.java
specifies that the first directory in our package name ( com ) should be placed in the parent
directory—we did this for all the reusable classes in this chapter. Once you compile with
the -d option, the package's datastructures directory contains the files ListNode.class ,
List.class and EmptyListException.class .
Step 4: Importing Types from Your Package
Once types are compiled into a package, they can be imported ( Step 4 ). Class ListTest
(Fig. 21.5) is in the default package because its .java file does not contain a package dec-
laration. Because class ListTest is in a different package from List and EmptyListExcep-
tion , you must either import these classes so that class ListTest can use them (lines 3-4
of Fig. 21.5) or you must fully qualify the names List and EmptyListException every-
where they're used throughout class ListTest . For example, line 10 of Fig. 21.5 could
have been written as:
com.deitel.datastructures.List<Integer> list =
new com.deitel.datastructures.List<>();
 
Search WWH ::




Custom Search