Creating a Package


package com.timothyfisher.book;



In a large application or library, Java classes are usually organized into packages. To put a class into a package, you simply include a package statement, such as the one shown in this phrase, at the beginning of the class file. The package statement must be the first non-comment line of a class file. The example in this phrase would assign the class contained in the file to the package com.timothyfisher.book.

The package name of a class becomes a part of its full name. For example, if we had a class named MathBook in the com.timothyfisher.book package, the fully specified class name would be com.timothyfisher.book.MathBook. Package names also dictate the directory structure in which the class source files are stored. Each element of the path name represents a directory. For example, if your source code root directory is at project/src, the source code for the MathBook class would be stored in the following directory path:

project/src/com/timothyfisher/book/


The Java standard libraries are all organized into packages that you are most likely familiar with. These packages include java.io, java.lang, java.util, and so on.

Classes stored in a package can also be easily imported into a file. For example, you can import an entire package into your Java source file with the following syntax:

import java.util.*;


This import statement will import all the classes contained in the java.util package. Be aware, though, that this will not import classes contained in packages beneath java.util, such as those contained in the package java.util.logging. A separate import statement is needed to also import those classes.

Java 5.0 introduced a new feature related to importing classes called static imports. A static import allows you to import static members from classes, allowing them to be used without class qualification. For example, to reference the cos() method in the java.lang.Math package, you would have to refer to it as follows:

double val = Math.cos(90);


If you import the java.lang.Math package using a static import like this:

import static java.lang.Math.*;


you can refer to the cos() method as follows:

double val = cos(90);


When executing a Java application from the command line using the Java executable, you must include the full package name when specifying the main executable class. For example, to run a main() method in the MathBook example discussed previously, you would type the following:

java com.timothyfisher.book.MathBook


This command would be executed from the root of the package structure. In this case, the directory above the com directory.

Classes that are not specifically assigned to a package using a package statement are considered to be included in a "default" package. It is a good practice to always put your classes in packages that you define. Classes that are in the default package can not be imported or used within classes in other packages.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net