7.10. Packages, Classpath, and JAR Archives

Java lets you group class libraries into separate modules or “packages” to avoid naming conflicts and to simplify their handling. For instance, in a real project, the Ship and Shape class hierarchies would have been easier to create and use if placed in separate packages. To create a package called packagename, first make a subdirectory with the same name and place all source files there. Each of these files should contain

package packagename;

as the first noncomment line. Files that lack a package declaration are automatically placed in an unnamed package. Files in the main directory that want to use the package should include

import packagename.ClassName;


import packagename.*;

before the ...

Get Core Web Programming, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.