Flash CS3 Documentation |
|||
| ActionScript 2.0 Language Reference > ActionScript language elements > Statements > static statement | |||
class someClassName{
static var name;
static function name() {
// your statements here
}
}
Specifies that a variable or function is created only once per class rather than being created in every object based on that class.
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, but only through a non-static function that accesses the static member.
You can use this keyword in class definitions only, not in interface definitions.
Availability: ActionScript 2.0; Flash Player 6
name:String - The name of the variable or function that you want to specify as static.
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);
Flash CS3