Defining packages

To put a class inside a package, the package keyword is used. If specified, it has to be the first non-commented line in the code. Like all statements in Java, it has to end with a semicolon (;). Let's look an example:

    package com.example.package_name;

We discussed packages thoroughly in the previous chapter, including naming conventions and requirements. When no package line is present, the class is put in what is called the default package.

By convention, the directory structure of the Java source code files should match the package names. All popular IDEs understand the package structure and will not show individual subdirectories but the full package names instead. As an example, here's a screenshot of the Eclipse IDE's project explorer: