Defining a Package


Assigning the contents of a source file (called a compilation unit in Java) to a package is easy. You simply include a package declaration at the top of the source file. The declaration consists of the package keyword followed by the package name .

 package package_name; 

The classes and interfaces contained in the source file will be associated with the specified package. Subpackages may also be used and follow a hierarchical naming structure, where the package levels are separated by periods.

 package package1.package2.package3; 

The java.awt.event and javax.swing.table packages are examples of package hierarchies in the Java API.

Packages are typically mapped to a directory hierarchy. The files corresponding to a given package must be placed in a directory hierarchy that has the same name as the package. For example, the files that make up the ThermoData.AirSpecies package would be placed in an AirSpecies directory that would be a subdirectory of a ThermoData directory.

The bytecode files of a package must be compiled in their appropriate directories from the package root directory. For example, if an O2 class was defined in the ThermoData.AirSpecies package and the root directory of the package was named MyData, you would place the O2 class source code in the ThermoData\AirSpecies directory and compile it from the MyData directory using the syntax

 javac ThermoData\AirSpecies\O2.java 

Example: Including Classes in Packages

Let's create a package named Fluids.Gas . We will define a Species class and an IonizedSpecies class as members of the Fluids.Gas package. The classes are assigned to a package by placing a package declaration at the beginning of the source file. The Species.java code listing follows ”

 package Fluids.Gas; public class Species {   private String name;   private double molarMass;   public Species(String name, double molarMass) {     this.name = name;     this.molarMass = molarMass;   }   public String getName() {     return name;   }   public double getMolarMass() {     return molarMass;   } } 

Here is the IonizedSpecies.java code listing ”

 package Fluids.Gas; public class IonizedSpecies extends Species {   private int chargeLevel;   public IonizedSpecies(String nm, double mw,                          int charge) {     super(nm, mw);     chargeLevel = charge;   }   public int getChargeLevel() {     return chargeLevel;   } } 

To physically associate the .class files with the package, the Species.java and IonizedSpecies.java source codes should be placed in the Fluids\Gas (or Fluids/Gas or Fluids:Gas) directory below the current working directory. The files should be compiled from the current working directory using the syntax

 javac Fluids\Gas\Species.java javac Fluids\Gas\IonizedSpecies.java 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

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