12.6 Packages

 < Day Day Up > 



12.6 Packages

Packages are considered in a separate section because they represent the major structural element for source code in Java. Many concepts such as scope resolution modifiers and program distribution using Java ARchive (JAR) files are built around packages. Packages are covered here in three parts. Section 12.6.1 gives an overview of packages, Section 12.6.2 talks about scope resolution modifiers in relation to packages, and Section 12.6.3 discusses distribution of a program and the use of JAR files and the CLASSPATH environment variable.

12.6.1 What Are Packages?

A package is a collection of the ".class" files for related classes that are stored and distributed together in a program. For example, the classes that make up the Java AWT are all part of the java.awt package, and all the classes that make up the Java utilities are in the class java.util package. All files containing classes that are part of a package must be in the same directory and have the "package" clause as the first statement in the file. All packages that are used by a program must be referenced as an offset from a directory contained in the CLASSPATH system variable. By using classes, Java has forced programmers to distribute programs using a standard structure. In Java, a class that is being used outside of a package must also be declared public, forcing the source file to have the same name as the ".class" file that is created. This makes it easier to manage the final artifact program and maintain the source files for the program.

To see how this works, first look at the useAnimator.bat file that comes with the source code for this text and is shown in Exhibit 4 (Program12.2). In this batch file, the CLASSPATH is set to the parent directory of the animator package used in Chapters 7 to 9. Later, when the Java source files contain the statement "import animator," the directories under all entries in the CLASSPATH variable are searched for a directory named "animator". The first such directory found is assumed to be the animator package, and any classes in the animator package will resolve to a public class file in this directory. (See Exhibit 5.)

Exhibit 4: Program12.2

start example

 public class IntStack {      int sizeOfStack;      int elements[];      int currentElement;      /**       *  The exception to be thrown if this stack is full and an element is       *  to be added.       */      public class FullException extends Exception {           public FullException() {                super();           }           public FullException(String message) {                super(message);           }      }      /**       *  The exception to be thrown if this stack is empty and an element       * is to be retrieved.       */      public class EmptyException extends Exception {           public EmptyException() {                super();           }           public EmptyException(String message) {                super(message);           }      }      /**     * Public constructor that creates a stack of "sizeOfStack" ints. */      public IntStack(int sizeOfStack) {           this.sizeOfStack = sizeOfStack;           this.elements = new int[sizeOfStack];           this.currentElement = -1;      } /**      *  Default public constructor, creates a stack of 10 ints.        */      public IntStack() {           this(10);      }      /**       *   Look at the item at the top of the stack.       */      public int peek() throws IntStack.EmptyException {           if (currentElement < 0)                throw new IntStack.EmptyException();           return elements[currentElement];      } /**       * Put a new item on the stack.       */      public void push(int newElement) throws IntStack.FullEx- ception {           int tmpElement = currentElement + 1;           if (tmpElement >= sizeOfStack)                throw new IntStack.FullException();           elements[++currentElement] = newElement;      }      /**       *   Remove and return the item at top of the stack.       */      public int pop() throws IntStack.EmptyException {           if (currentElement < 0)                throw new IntStack.EmptyException();           return elements[currentElement--];      }      /**       *   Test program.       */      public static void main(String args[]) {           try {                IntStack intStack = new IntStack(5);                intStack.push(5);                intStack.push(2);                System.out.println(intStack.pop());                System.out.println(intStack.pop());                intStack.peek();           } catch (Exception e) {                e.printStackTrace();           }           try {                IntStack intStack = new IntStack(5);                intStack.push(5);                intStack.push(2);                System.out.println(intStack.pop());                System.out.println(intStack.pop());                intStack.pop();           } catch (Exception e) {                e.printStackTrace();           }           try {                IntStack intStack = new IntStack(5);                intStack.push(1);                intStack.push(2); intStack.push(3);                intStack.push(4);                intStack.push(5);                intStack.push(6);           } catch (IntStack.FullException e) {                e.printStackTrace(); }      } } 

end example

Exhibit 5: useAnimator.bat

start example

 set CLASSPATH=%CLASSPATH%;.;%cd% 

end example

This organization also applies to the source files for the animator. All source files must be in a directory named "animator" that is in the CLASSPATH for the program to compile. This means that the source code for all classes used outside of a package must be in a file named the same as the class and in a directory with all other files from that package. This means that Java programs have enforced order, making it more difficult (but possible) to misplace source code.

12.6.2 Scope Resolution Modifiers

Another reason to use packages in Java is that they form the basis for the scope of variables outside of classes. Java has four values for scope of a variable. The scope modifiers are listed below in decreasing order of restrictiveness:

  • Private - A private scope for a variable means that this variable, method, or inner class/interface cannot be used outside of the current class. Note that a normal class/interface cannot be declared private, as it could never be used.

  • Friendly - Friendly scope is the default scope if no scope modifier is used. Friendly is not a keyword in Java and so cannot be used explicitly in Java. Friendly scope says that this variable, method, inner class/interface, or class/interface can be referenced in any other class in the current package.

  • Protected - Protected scope indicates that the variable, method, or inner class/interface can be accessed in the current package or in any descendent of the current class. Note once again that it would not make sense to allow classes/interfaces to be declared protected.

  • Public - Public scope allows a variable, method, inner class/ interface, or class/interface to be used anywhere in a program.

12.6.3 Distributing Source Programs

Packages serve one more very useful purpose in Java as the basis for distributing the applications. Most applications in Java are packaged into JAR files, which are basically zip files that have some extra information about the Java classes they contain. The files inside of the

JAR file are stored in directories representing the packages in the file. The CLASSPATH environment variable is then set to the JAR file, and the program can be distributed. This can also be made to work with the code base for an applet and is used in Chapter 13 with RMI.



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

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