Beginning Programming with Java For Dummies
Book image
Explore Book Buy On Amazon

A package is a group of classes that belong together. Without packages, the entire universe of Java classes would be a huge, unorganized mess. Creating your own packages to hold your classes is easy. Well, relatively easy, anyway. You must go through a few steps:

  1. Pick a name for your package.

    You can use any name you wish, but it’s a good idea to follow the established convention of using your Internet domain name (if you have one), only backwards. (Using your domain name backwards ensures that your package names are unique.)

    Notice that package names are in all-lowercase letters. That’s not an absolute requirement, but it’s a Java convention that you ought to stick to. If you start using capital letters in your package names, you’ll be branded a rebel for sure. And since Java is case-sensitive, a package named com.lowewriter is a different package from one named com.LoweWriter.

    You can add additional levels beyond the domain name if you want.

    If you don’t have a domain all to yourself, try using your e-mail address backwards. For example, if your e-mail address is [email protected], use com.somecompany.somebody for your package names.

    That way they are still unique. (If you ever want to distribute your Java packages, you should register a domain name. Nothing says "Amateur" like a package name that starts with com.aol. And, although a package name created from a backwards e-mail address is almost sure to be unique, there is a small chance that it won’t be.)

  2. Choose a directory on your hard drive to be the root of your class library.

    You need a place on your hard drive to store your classes. Create a directory such as c:javaclasses.

    This folder becomes the root directory for your Java packages.

  3. Create subdirectories within the root directory for your package name.

    For example, for the package named com.lowewriter.util, create a directory named com in the c:javaclasses directory (assuming that’s the name of your root). Then, in the com directory, create a directory named lowewriter. Then, in lowewriter, create a directory named util. Thus, the complete path to the directory that contains the classes for the com.lowewriter.util package is c:javaclassescomlowewriterutil.

  4. Add the root directory for your package to the ClassPath environment variable.

    The exact procedure for doing this depends on your operating system. You can set the ClassPath by double-clicking System from the Control Panel. Click the Advanced tab, and then click Environment Variables.

    Be careful not to disturb any directories already listed in the ClassPath. To add your root directory to the ClassPath, add a semicolon followed by the path to your root directory to the end of the ClassPath value. For example, suppose your ClassPath is already set to this:

    .;c:utilclasses

    Then you modify it to look like this:

    .;c:utilclasses;c:javaclasses

    Here ;c:javaclasses was added to the end of the ClassPath value.

  5. Save the files for any classes you want to be in a particular package in the directory for that package.

    For example, save the files for a class that belongs to the com.lowewriter.util package in c:javaclassescomlowewriterutil.

  6. Add a package statement to the beginning of each source file that belongs in a package.

    The package statement simply provides the name for the package that any class in the file is placed in. For example:

    package com.lowewriter.util;

    The package statement must be the first non-comment statement in the file.

Suppose you’ve developed a utility class named Console that has a bunch of handy static methods for getting user input from the console. For example, this class has a static method named askYorN that gets a Y or N from the user and returns a boolean value to indicate which value the user entered.

You decide to make this class available in a package named com.lowewriter.util so you and other like-minded programmers can use it in their programs. Here’s the source file for the Console class:

package com.lowewriter.util;
import java.util.Scanner;
public class Console
{
 static Scanner sc = new Scanner(System.in);
 public static boolean askYorN(String prompt)
 {
  while (true)
  {
   String answer;
   System.out.print("n" + prompt
   + " (Y or N) ");
   answer = sc.next();
   if (answer.equalsIgnoreCase("Y"))
    return true;
   else if (answer.equalsIgnoreCase("N"))
    return false;
  }
 }
}

Okay, so far this class has just the one method (askYorN), but one of these days you’ll add a bunch of other useful methods to it. In the meantime, you want to get it set up in a package so you can start using it right away.

So you create a directory named c:javaclassescomlowewriterutil and save the source file to this directory. Then you compile the program so the Console.class file is stored in that directory too. And you add c:javaclasses to your ClassPath environment variable.

Now you can use the following program to test that your package is alive and well:

import com.lowewriter.util.*;
public class PackageTest
{
 public static void main(String[] args)
 {
  while (Console.askYorN("Keep going?"))
  {
   System.out.println("D’oh!");
  }
 }
}

Here the import statement imports all the classes in the com.lowewriter.util package. Then, the while loop in the main method repeatedly asks the user if he or she wants to keep going.

About This Article

This article can be found in the category: