User Guide

Classes 119
Within the body of an instance method, both static and instance variables are in scope, which
means that variables defined in the same class can be referenced using a simple identifier. For
example, the following class, CustomArray, extends the Array class. The CustomArray class
defines a static variable named
arrayCountTotal to track the total number of class instances,
an instance variable named
arrayNumber that tracks the order in which the instances were
created, and an instance method named
getPosition() that returns the values of these
variables.
public class CustomArray extends Array
{
public static var arrayCountTotal:int = 0;
public var arrayNumber:int;
public function CustomArray()
{
arrayNumber = ++arrayCountTotal;
}
public function getArrayPosition():String
{
return ("Array " + arrayNumber + " of " + arrayCountTotal);
}
}
Although code external to the class must refer to the arrayCountTotal static variable
through the class object using
CustomArray.arrayCountTotal, code that resides inside the
body of the
getPosition() method can refer directly to the static arrayCountTotal
variable. This is true even for static variables in superclasses. Though static properties are not
inherited in ActionScript 3.0, static properties in superclasses are in scope. For example, the
Array class has a few static variables, one of which is a constant named
DESCENDING. Code
that resides in an Array subclass can refer to the static constant
DESCENDING using a simple
identifier.
public class CustomArray extends Array
{
public function testStatic():void
{
trace(DESCENDING); // output: 2
}
}
The value of the this reference within the body of an instance method is a reference to the
instance to which the method is attached. The following code demonstrates that the
this
reference points to the instance that contains the method:
class ThisTest
{
function thisValue():ThisTest