Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

The Java class libraries are divided into packages of related classes and interfaces. Most Java programs you build reuse existing packages, such as the Java class libraries, third party libraries or libraries developed within your organization. This tutorial shows how create your own packages of classes for reuse or distribution. The tutorial is intended for students or professionals who are alreadyfamiliar with building Java classes.

Download the code for this tutorial here.

[Note: This tutorial is an excerpt (Section 8.16) of Chapter 8, Class and Objects: A Deeper Look, from our textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.390-396. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

8.16 Time Class Case Study: Creating Packages (Continued)

Step 3: Compiling the Packaged Class

Step 3 is to compile the class so that it is stored in the appropriate package. When a Java file containing a package declaration is compiled, the resulting class file is placed in the directory specified by the package declaration. The package declaration in Fig. 8.18 indicates that class Time1 should be placed in the directory

com
deitel
jhtp6
ch08

The directory names in the package declaration specify the exact location of the classes in the package.

    When compiling a class in a package, the javac command-line option -d causes the javac compiler to create appropriate directories based on the class’s package declaration. The option also specifies where the directories should be stored. For example, in a command window, we used the compilation command

javac -d . Time1.java

to specify that the first directory in our package name should be placed in the current directory. The period (.) after -d in the preceding command represents the current directory on the Windows, UNIX and Linux operating systems (and several others as well). After executing the compilation command, the current directory contains a directory called com, com contains a directory called deitel, deitel contains a directory called sjhtp6 and sjhtp6 contains a directory called ch08. In the ch08 directory, you can find the file Time1.class. [Note: If you do not use the -d option, then you must copy or move the class file to the appropriate package directory after compiling it.]

    The package name is part of the fully qualified class name, so the name of class Time1 is actually com.deitel.sjhtp6.ch08.Time1. 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—Time1) in the program. If another package also contains a Time1 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).

Page 1 | 2 | 3 | 4 | 5
Return to Tutorial Index