Creating and using class members

To specify that a property or method is class member, or static, you use the static keyword. For example, the following StaticColor class declares a static variable named favorite_color, and a static method named getFavoriteColor(), which returns the value of favorite_color.

class StaticColor {
	static var favorite_color = "Red";

	static function getFavoriteColor() {
		return favorite_color;
	}
}

Static functions can only access static variables, not instance variables. For example, if you had forgotten to declare favorite_color as a static variable, as shown below, the Flash compiler would generate an error.

class StaticColor_error {
	var favorite_color = "Red"; 
	// favorite_color is an instance variable.

	static function getFavoriteColor() {
		return favorite_color; 
		// Compiler error! 
		// Static functions can only access static variables.
	}

}

To access a static variable, you specify the class name and then the member name, separated by a dot (.).

ClassName.classMember

Because static members aren't associated with an instance of a class, you can access them without creating an instance. For example, to call the getFavoriteColor() method of the StaticColor class, you would write the following code in a FLA or AS script:

var color = StaticColor.getFavoriteColor();

Example use: maintaining state

One use of static members is to maintain state for a class. For example, say you want to keep track of how many instances of a class have been created. While there are several ways you could probably do this, an easy way is to use a static variable that's incremented each time a new instance is created.

The following example code creates a Widget class that has a single, static member named widgetCount. Each time a new instance of the class is created, the value of widgetCount is incremented by one. Also, the current value of widgetCount is displayed in the Output panel.

To create an instance counter using a class variable:

  1. Create a new ActionScript (AS) file.
  2. Add the following code to the file:
  3. class Widget {
    	static var widgetCount:Number = 0; // initialize class variable
    	function Widget() {
    		trace("Creating widget #" + widgetCount);
    		count++;
    	}
    }
    

    The widgetCount variable is declared as static, and so initializes to zero only once. Each time the Widget class's constructor function is called, it adds one to widgetCount, and then displays the number of the current instance that's being created.

  4. Save the file as Widget.as.
  5. Create a new Flash (FLA) file and save it as createWidget.fla to the same directory as Widget.as.
  6. You'll create new instance of the Widget class in this file.

  7. In createWidget.fla, select Layer 1 in the Timeline and open the Actions panel (Window > Development Panels > Actions).
  8. Add the following code to the Actions panel.
  9. //Before you create any instances of the class, 
    //widgetCount is zero (0)
    trace("Widget count at start: " + Widget.widgetCount);
    var widget_1 = new Widget();
    var widget_2 = new Widget();
    var widget_3 = new Widget();
    
  10. Save the file, and then test it (Control > Test Movie).
  11. When run, you should see the following displayed in the Output panel:

    Widget count at start: 0
    Creating widget # 0
    Creating widget # 1
    Creating widget # 2