Variables

Posted: July 18, 2012 in Action Script tutorial
Tags:

Variables can be declared with either the var or const keywords. Variables declared with the var keyword can have their values changed multiple times throughout the execution of a script. Variables declared with the const keyword are called constants, and can have values assigned to them only once. An attempt to assign a new value to an initialized constant results in an error. 

Static variables

Static variables are declared using a combination of the static keyword and either the var or const statement. Static variables, which are attached to a class rather than an instance of a class, are useful for storing and sharing information that applies to an entire class of objects. For example, a static variable is appropriate if you want to keep a tally of the number of times a class is instantiated or if you want to store the maximum number of class instances that are allowed.

The following example creates a totalCount variable to track the number of class instantiations and a MAX_NUM constant to store the maximum number of instantiations. ThetotalCount and MAX_NUM variables are static, because they contain values that apply to the class as a whole rather than to a particular instance.

class StaticVars 
{ 
    public static var totalCount:int = 0; 
    public static const MAX_NUM:uint = 16; 
}

Code that is external to the StaticVars class and any of its subclasses can reference the totalCount and MAX_NUM properties only through the class itself. For example, the following code works:

trace(StaticVars.totalCount); // output: 0 
trace(StaticVars.MAX_NUM); // output: 16

You cannot access static variables through an instance of the class, so the following code returns errors:

var myStaticVars:StaticVars = new StaticVars(); 
trace(myStaticVars.totalCount); // error 
trace(myStaticVars.MAX_NUM); // error

Variables that are declared with both the static and const keywords must be initialized at the same time as you declare the constant, as the StaticVars class does forMAX_NUM. You cannot assign a value to MAX_NUM inside the constructor or an instance method. The following code will generate an error, because it is not a valid way to initialize a static constant:

// !! Error to initialize static constant this way 
class StaticVars2 
{ 
    public static const UNIQUESORT:uint; 
    function initializeStatic():void 
    { 
        UNIQUESORT = 16; 
    } 
}

Instance variables

Instance variables include properties declared with the var and const keywords, but without the static keyword. Instance variables, which are attached to class instances rather than to an entire class, are useful for storing values that are specific to an instance. For example, the Array class has an instance property named length, which stores the number of array elements that a particular instance of the Array class holds.

ActionScript 3.0 supports the const statement, which you can use to create constants. Constants are properties with a fixed value that cannot be altered. You can assign a value to a constant only once, and the assignment must occur in close proximity to the declaration of the constant. For example, if a constant is declared as a member of a class, you can assign a value to that constant only as part of the declaration or inside the class constructor.

Comments
  1. pankaj says:

    this tuts are good…..I want for beginer purpose…..like some intractivities …..

    • Hi,
      Thnax for the query as per the beginning something I would like to tell you the the variable are the storage location where you can store the data which can change itself anywhere in your code.
      For an example i declare a variable with some name of that.
      var name:MovieClip
      var name:Number
      var str:String
      var arr:Array;
      Thanks

Leave a comment