User Guide
42 ActionScript Language and Syntax
When a package is created, the default access specifier for all members of that package is
internal, which means that, by default, package members are only visible to other members
of that package. If you want a class to be available to code outside the package, you must
declare that class to be
public. For example, the following package contains two classes,
SampleCode and CodeFormatter:
// SampleCode.as file
package samples
{
public class SampleCode {}
}
// CodeFormatter.as file
package samples
{
class CodeFormatter {}
}
The SampleCode class is visible outside the package because it is declared as a public class.
The CodeFormatter class, however, is visible only within the samples package itself. If you
attempt to access the CodeFormatter class outside the samples package, you will generate an
error, as the following example shows:
import samples.SampleCode;
import samples.CodeFormatter;
var mySample:SampleCode = new SampleCode(); // okay, public class
var myFormatter:CodeFormatter = new CodeFormatter(); // error
If you want both classes to be available outside the package, you must declare both classes to
be
public. You cannot apply the public attribute to the package declaration.
Fully qualified names are useful for resolving name conflicts that may occur when using
packages. Such a scenario may arise if you import two packages that define classes with the
same identifier. For example, consider the following package, which also has a class named
SampleCode:
package langref.samples
{
public class SampleCode {}
}
If you import both classes, as follows, you will have a name conflict when referring to the
SampleCode class:
import samples.SampleCode;
import langref.samples.SampleCode;
var mySample:SampleCode = new SampleCode(); // name conflict