Using packages

You can organize your ActionScript class files in packages. A package is a directory that contains one or more class files, and that resides in a designated classpath directory. (See Understanding the classpath.) A package can, in turn, contain other packages, called subpackages, each with its own class files.

Package names must be identifiers; that is the first character must be a letter, underscore (_), or dollar sign ($), and each subsequent character must be a letter, number, underscore, or dollar sign.

Packages are commonly used to organize related classes. For example, you might have three related classes, Square, Circle, and Triangle, that are defined in Square.as, Circle.as, and Triangle.as. Assume that you've saved the AS files to a directory specified in the classpath.

// In Square.as:
class Square {}

// In Circle.as:
class Circle {}

// In Triangle.as:
class Triangle {}

Because these three class files are related, you might decide to put them in a package (directory) called Shapes. In this case, the fully qualified class name would contain the package path, as well as the simple class name. Package paths are denoted with dot syntax, where each dot indicates a subdirectory.

For example, if you placed each AS file that defines a shape in the Shapes directory, you would need to change the name of each class file to reflect the new location, as follows:

// In Shapes/Square.as:
class Shapes.Square {}

// In Shapes/Circle.as:
class Shapes.Circle {}

// In Shapes/Triangle.as:
class Shapes.Triangle {}

To reference a class that resides in a package directory, you can either specify its fully qualified class name or import the package by using the import statement. For more information, see Importing classes.