User Guide

206 Chapter 5: ActionScript Core Language Elements
static
Availability
Flash Player 6.
Usage
class someClassName{
static var name;
static function name() {
// your statements here
}
}
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” on page 60.
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:
trace(Users.instances);
var user1:Users = new Users();
trace(Users.instances);
var user2:Users = new Users();
trace(Users.instances);
See also
private, public
CHAPTER 5
ActionScript Core Language Elements