Package Statement

The package statement identifies the package that a java program belongs to. If your program does not include a package statement, the program belongs to the default package, which is simply a package that has no name. This is acceptable for short programs written for testing purposes. But if you plan to distribute your program, you should create a package for the program and use a package statement in all of the program’s source files to identify the package.

The syntax of the package statement is simple:

package package-name;

Package-name refers to the name of the package that the program should be added to. You can use any name you wish, but I recommend you follow the established convention of transposing your Internet domain name (if you have one). I own a domain called LoweWriter.com, so I use the name com. lowewriter for all my packages. (Transposing your domain name ensures that your package names are unique.)

Notice that package names are in lowercase letters. That’s not an absolute requirement, but it’s a Java convention that you ought to stick to.

tip.eps You can add additional levels beyond the domain name if you want. For example, I put my utility classes in a package named com.lowewriter.util. Any class that I want to include in this package must include the following statement:

package com.lowewriter.util;

After you decide on a package name, you can create ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.