Skip to content

Packaging Classes

Estimated time to read: 1 minute

Overview

Membership in a package is determined by a package statement. The package statement is the first, non comment, statement in the file.

package io.entityfour.books; // This line is the package statement

public class Book(){
 // Code here
}

Mapping Packages to the File System

  • Packages must be mapped onto the file system of the computer where they are being used.
  • This is accomplished by passing a CLASSPATH variable to the JVM, see 202 - Packages and the Classpath for more information.

The JVM translates the package statements in Java code into platform specific statements that are used to locate the class it is seeking.

Creating Packages

  • Create a directory with the same name as that of the package
  • Define all the classes and interfaces that are to be put in the package and save as .java files in the directory
  • Within each source file, declare the package at the beginning of each .java file using the package keyword followed by a package name.

Package Naming

By convention, packages are named using a reverse internet address convention. However, this is merely a convention, if you wish to throw caution to the wind, you can... but don't.

package io.entityfour.project