User Guide

38 ActionScript Language and Syntax
Packages and namespaces
Packages and namespaces are related concepts. Packages allow you to bundle class definitions
together in a way that facilitates code sharing and minimizes naming conflicts. Namespaces
allow you to control the visibility of identifiers, such as property and method names, and can
be applied to code whether it resides inside or outside a package. Packages let you organize
your class files, and namespaces let you manage the visibility of individual properties and
methods.
Packages
Packages in ActionScript 3.0 are implemented with namespaces, but are not synonymous with
them. When you declare a package, you are implicitly creating a special type of namespace
that is guaranteed to be known at compile time. Namespaces, when created explicitly, are not
necessarily known at compile time.
The following example uses the
package directive to create a simple package containing one
class:
package samples
{
public class SampleCode
{
public var sampleGreeting:String;
public function sampleFunction()
{
trace(sampleGreeting + " from sampleFunction()");
}
}
}
The name of the class in this example is SampleCode. Because the class is inside the samples
package, the compiler automatically qualifies the class name at compile time into its fully
qualified name: samples.SampleCode. The compiler also qualifies the names of any properties
or methods, so that
sampleGreeting and sampleFunction() become
samples.SampleCode.sampleGreeting and samples.SampleCode.sampleFunction(),
respectively.