MACROMEDIA FLEX-FLEX ACTIONSCRIPT LANGUAGE Reference page 61

Actionscript language reference
Table of Contents

Advertisement

Class members are declared with the
class member with the following code:
species
class Person
{
static var species:String = "Homo sapiens";
...
}
You can also declare methods of a class to be static, as shown in the following code:
static function functionName() {
// function body
}
Class (static) methods can access only class (static) properties, not instance properties. For
example, the following code will result in a compiler error because the class method
references the instance variable
class StaticTest {
var name:String="Ted";
static function getName():Void {
var local_name:String = name;
// Error! Instance variables cannot be accessed in static functions.
}
}
To solve this problem, you could either make the method an instance method or make the
variable a class variable.
A common use of class members is the Singleton design pattern. The Singleton design pattern
makes sure that a class has only one instance and provides a way of globally accessing the instance.
For more information on the Singleton design pattern, see
coldfusion/articles/design_patterns.html.
Often there are situations when you need exactly one object of a particular type in a system. For
example, in a chess game, there is only one chessboard, and in a country, there is only one capitol
city. Even though there is only one object, it is attractive to encapsulate the functionality of this
object in a class. However, you might need to manage and access the one instance of that object.
Using a global variable is one way to do this, but global variables are often not desirable. A better
approach is to make the class manage the single instance of the object itself using class members,
such as the following example:
class Singleton {
private var instance:Singleton = null;
public function doSomething():Void {
//...
}
public static function getInstance():Singleton {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
modifier. For example, you could declare the
static
:
name
www.macromedia.com/devnet/mx/
Instance and class members
getName()
61

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents