User Guide

static 787
static
Availability
Flash Player 6.
Usage
class someClassName{
static var name;
static function name() {
// your statements here
}
}
Note: To use this keyword, you must specify ActionScript 2.0 and Flash Player 6 or later in the Flash
tab of your FLA file’s Publish Settings dialog box. This keyword is supported only when used in
external script files, not in scripts written in the Actions panel.
Parameters
name
The name of the variable or function that you want to specify as static.
Description
Keyword; specifies that a variable or function is created only once per class rather than being
created in every object based on that class. For more information, see “Instance and class
members” in Using ActionScript in Flash. You can access a static class member without creating an
instance of the class by using the syntax
someClassName.name. If you do create an instance of the
class, you can also access a static member using the instance.
You can use this keyword in class definitions only, not in interface definitions.
Example
The following example demonstrates how you can use the static keyword to create a counter
that tracks how many instances of the class have been created. Because the
numInstances variable
is static, it will be created only once for the entire class, not for every single instance. Create a new
AS file called Users.as and enter the following code:
class Users {
private static var numInstances:Number = 0;
function Users() {
numInstances++;
}
static function get instances():Number {
return numInstances;
}
}
Create a FLA or AS document in the same directory, and enter the following ActionScript in
Frame 1 of the Timeline:
trace(Users.instances);
var user1:Users = new Users();
trace(Users.instances);
var user2:Users = new Users();
trace(Users.instances);
CHAPTER 2
ActionScript Language Reference