User Guide
Classes 115
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); // 0
trace(StaticVars.MAX_NUM); // 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 that 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” on page 120.
Methods
Methods are functions that are part of a class definition. Once an instance of the class is
created, a method is bound to that instance. Unlike a function declared outside a class, a
method cannot be used apart from the instance to which it is attached.