O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Constants

A constant is a static variable, instance variable, or local variable with a value that, once initialized, remains fixed for the remainder of the program. To create a constant, we use standard variable-definition syntax, but with the keyword const instead of var. By convention, constants are named with all capital letters. To create a constant static variable, we use the following generalized code directly within a class body:

static const IDENTIFIER = value

To create a constant instance variable, we use the following generalized code directly within a class body:

const IDENTIFIER = value

To create a constant local variable, we use the following generalized code within a method or function:

const IDENTIFIER = value

In the preceding three code examples, IDENTIFIER is the name of the constant, and value is the variable's initial value. For constant static variables and constant local variables, once value has been assigned by the variable initializer, it can never be reassigned.

For constant instance variables, if the program is compiled in strict mode, once value has been assigned by the variable initializer, it can never be reassigned. If the program is compiled in standard mode, after value has been assigned by the variable initializer, the variable's value can also be assigned within the constructor function of the class containing the variable definition, but not thereafter. (We'll learn the difference between strict mode and standard mode compilation in Chapter 7.)

Constants are typically used to create static variables whose fixed values define the options for a particular setting in a program. For example, suppose we're building an alarm clock program that triggers a daily alarm. The alarm has three modes: visual (a blinking icon), audio (a buzzer), or both audio and visual. The alarm clock is represented by a class named AlarmClock. To represent the three alarm modes, the AlarmClock class defines three constant static variables: MODE_VISUAL, MODE_AUDIO, and MODE_BOTH. Each constant is assigned a numeric value corresponding to its mode. Mode 1 is considered "visual mode," mode 2 is considered "audio mode," and mode 3 is considered "both visual and audio mode." The following code shows the definitions for the mode constants:

public class AlarmClock {
  public static const MODE_VISUAL = 1;
  public static const MODE_AUDIO  = 2;
  public static const MODE_BOTH   = 3;
}

To keep track of the current mode for each AlarmClock instance, the alarm clock class defines an instance variable, mode. To set the mode of an AlarmClock object, we assign one of the mode constants' values (1, 2, or 3) to the instance variable mode. The following code sets the default mode for new AlarmClock objects to audio-only (mode 2):

public class AlarmClock {
  public static const MODE_VISUAL = 1;
  public static const MODE_AUDIO  = 2;
  public static const MODE_BOTH   = 3;

  private var mode = AlarmClock.MODE_AUDIO;
}

When it comes time to signal an alarm, the AlarmClock object takes the appropriate action based on its current mode. The following code shows how an AlarmClock object would use the mode constants to determine which action to take:

public class AlarmClock {
  public static const MODE_VISUAL = 1;
  public static const MODE_AUDIO  = 2;
  public static const MODE_BOTH   = 3;

  private var mode = AlarmClock.MODE_AUDIO;

  private function signalAlarm () {
    if (mode == MODE_VISUAL) {
      // Display icon
    } else if (mode == MODE_AUDIO) {
      // Play sound
    } else if (mode == MODE_BOTH) {
      // Display icon and play sound
    }
  }
}

Note that in the preceding code, the mode constants are not technically necessary. Strictly speaking, we could accomplish the same thing with literal numeric values (magic values). However, the constants make the purpose of the numeric values much easier to understand. For comparison, the following code shows the AlarmClock class implemented without constants. Notice that, without reading the code comments, the meaning of the three mode values cannot easily be determined.

public class AlarmClock {
  private var mode = 2;

  private function signalAlarm () {
    if (mode == 1) {
      // Display icon
    } else if (mode == 2) {
      // Play sound
    } else if (mode == 3) {
      // Display icon and play sound
    }
  }
}

Now let's move on to the counterpart of static variables: static methods.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required