18.1. Package Naming
A package name should prevent collisions with other packages, so choosing a name that's both meaningful
and unique is an important aspect of package design. But with programmers around the globe developing
packages, there is no way to find out who is using what package names. So choosing unique package names is
a problem. If you are certain a package will be used only inside your organization, you can use an internal
arbiter to ensure that no projects pick clashing names.
But in the world at large, this approach is not practical. Package identifiers are simple names. A good way to
ensure unique package names is to use an Internet domain name. If you worked at a company named Magic,
Inc., that owned the domain name magic.com, the attribute package declaration should be
package com.magic.attr;
Notice that the components of the domain name are reversed from the normal domain name convention.
If you use this convention, your package names should not conflict with those of anyone else, except possibly
within your organization. If such conflicts arise (likely in a large organization), you can further qualify using a
more specific domain. Many large companies have internal subdomains with names such as east and
europe. You could further qualify the package name with such a subdomain name:
package com.magic.japan.attr;
Package names can become quite long under this scheme, but it is relatively safe. No one else using this
technique will choose the same package name, and programmers not using the technique are unlikely to pick
your names.
18.2. Type Imports
Type imports are a generalization of the static imports that you learned about in Chapter 2 on page 71. They
allow you to refer to types by their simple names instead of having to write their fully qualified name.
When you write code outside a package that needs types declared within that package you have two options.
One is to use the fully qualified name of the type. This option is reasonable if you use only a few items from a
package, but given the long names that packages tend to acquire (or even the shorter ones in the standard
libraries) it can be quite tedious to use fully qualified names.
The other way to use types from a package is to import part or all of the package. A programmer who wants to
use the attr package could put the following line near the top of a source file (after any package
declaration but before anything else):
import attr.*;
Now the types in that package can be referred to simply by name, such as Attributed. An import that uses
a * is called an import on demand declaration. You can also perform a single type import:
import attr.Attributed;