User Guide
114 Object-Oriented Programming in ActionScript
Static properties are not inherited by subclasses, but the properties are part of a subclass’s
scope chain. This means that within the body of a subclass, a static variable or method can be
used without referencing the class in which it was defined. For more information, see “Static
properties not inherited” on page 136.
User-defined namespace attributes
As an alternative to the predefined access control attributes, you can create a custom
namespace for use as an attribute. Only one namespace attribute can be used per definition,
and you cannot use a namespace attribute in combination with any of the access control
attributes (
public, private, protected, internal). For more information about using
namespaces, see “Namespaces” on page 43.
Variables
Variables can be declared with either the var or const keywords. Variables declared with the
var keyword can have their values changed multiple times throughout the execution of a
script. Variables declared with the
const keyword are called constants, and can have values
assigned to them only once. An attempt to assign a new value to an initialized constant results
in an error. For more information, see “Constants” on page 76.
Static variables
Static variables are declared using a combination of the static keyword and either the var
and
const statements. Static variables, which are attached to a class rather than an instance of
a class, are useful for storing and sharing information that applies to an entire class of objects.
For example, a static variable is appropriate if you want to keep a tally of the number of times
a class is instantiated or if you want to store the maximum number of class instances that are
allowed.
The following example creates a
totalCount variable to track the number of class
instantiations and a
MAX_NUM constant to store the maximum number of instantiations. The
totalCount and MAX_NUM variables are static because they contain values that apply to the
class as a whole rather than to a particular instance.
class StaticVars
{
public static var totalCount:int = 0;
public static const MAX_NUM:uint = 16;
}