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.

Static variables

Static variables are declared using a combination of the static keyword and either the var or const statement. 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; 

Code that is external to the StaticVars class and any of its subclasses can reference the totalCount and MAX_NUM properties only through the class itself. For example, the following code works:

trace(StaticVars.totalCount); // output: 0 
trace(StaticVars.MAX_NUM); // output: 16

You cannot access static variables through an instance of the class, so the following code returns errors:

var myStaticVars:StaticVars = new StaticVars(); 
trace(myStaticVars.totalCount); // error 
trace(myStaticVars.MAX_NUM); // error

Variables that are declared with both the static and const keywords must be initialized at the same time as you declare the constant, as the StaticVars class does for MAX_NUM. You cannot assign a value to MAX_NUM inside the constructor or an instance method. The following code will generate an error, because it is not a valid way to initialize a static constant:

// !! Error to initialize static constant this way 
class StaticVars2 
    public static const UNIQUESORT:uint; 
    function initializeStatic():void 
        UNIQUESORT = 16; 

Instance variables

Instance variables include properties declared with the var and const keywords, but without the static keyword. Instance variables, which are attached to class instances rather than to an entire class, are useful for storing values that are specific to an instance. For example, the Array class has an instance property named length, which stores the number of array elements that a particular instance of the Array class holds.

Instance variables, whether declared as var or const, cannot be overridden in a subclass. You can, however, achieve functionality that is similar to overriding variables by overriding getter and setter methods. For more information, see Get and set accessor methods.