In a class in ActionScript you can also create static elements (variable, methods, constante). Static elements belong exclusively to a class, and can not be accessed through an instance of the class, but directly using the name of the class.
• To create static elements, you use the keyword static in the definition of the element.

Static properties

Variables defined in a class are associated with properties that can be accessed through an instance of that class (if they have the "public" attribute), with this syntax:   instance_name.property


Variables, or static properties are created by adding the keyword static before keyword var in the variable definition:
attribute static var prop_name:DataType = value;
Static properties belong to the class and can be accessed with this syntax:
ClassName.prop_name
Here's an example. We create a class (named "elStatic") with two public properties, the second property is defined as static. - If you do not know how to create a class in AS3 and use it in Flash, first study the lesson: OOP - Classes and objects - Create Class
package {
  // Define "elStatic" class
  public class elStatic
  {
    // Define properties
    public var prop1:String = 'coursesweb.net';
    public static var prop2:String = 'ActionScript';         // Static variable
  }
}
- Open a new Flash document, in "Actions panel" add the following code, then save this document in the same folder as the file with this class, "elStatic.as".
// Create an instance of the "elStatic" class
var obj:elStatic = new elStatic();

// access the class property 
trace(obj.prop1);            // coursesweb.net
// trace(obj.prop2);            // Will generate an error

// acces 'prop2' directly through the class name
trace(elStatic.prop2);       // ActionScript
- Press "Ctrl+Enter to see the result in Output.
- If you delete the backslashes (//) in front of the expression "trace(obj.prop2);", it will generate an error. "prop2" having the "static" attribute, is not recognized in the instance of the class ("obj"), but can be accessed directly through the class name (elStatic.prop2).
To access a static variable inside the class code, you can use its name, or the expression "ClassName.static_prop".

Static constants

Constants are variables with a fixed value, they are defined with the instruction const, and their value can not be changed.
Static constants are created by adding the keyword static before keyword const.
- To access a static constant, use the expression:

ClassName.CONSTANT
- Inside the class, static constants can be accessed directly with their name or with the syntax above.

In the following example (continuing the one above) we add 2 constants, both with "public" attribute. The second constant is defined as static.
package {
  // Defining "elStatic" class
  public class elStatic
  {
    // Defining the properties
    public var prop1:String = 'coursesweb.net';
    public static var prop2:String = 'ActionScript';         // Static variable

    // Defining constants
    public const TUTORIALS:String = 'web development';
    public static const SITE:String = 'coursesweb';            // Static constant
  }
}
- In the Flash document add the following code:
// Create instance of the class "elStatic"
var obj2:elStatic = new elStatic();

// access the class constants
trace(obj2.TUTORIALS);            // web development
// trace(obj2.SITE);            // Returns an error

// access 'SITE' using the class name
trace(elStatic.SITE);       // coursesweb
- If you delete the backslashes (//) in front of the expression "trace(obj2.SITE);", it will generate an error. The "SITE" constant, having the "static" attribute, is not recognized in the instance of the class ("obj2"), but can be accessed through the class name (elStatic.SITE).

Static Methods

To create a static method, add the keyword static before the keyword function, in the method definition.

attribute static function methodName() {
  // code to execute
}

To call a static method, use this syntax:
ClassName.methodName(parameters)
- In the body of a static method you can only use other static elements (properties, constants, methods). The keyword "this" can not be used.

Let's continue the previous example, we add a static method (named "Courses") in the "elStatic" class, then we access it in a script (see the explanations in code).
package {
  // Defining the "elStatic" class
  public class elStatic
  {
    // Defining properties
    public var prop1:String = 'coursesweb.net';
    public static var prop2:String = 'ActionScript';         // Static variable

    // Defining constants
    public const TUTORIALS:String = 'web';
    public static const SITE:String = 'coursesweb';            // Static constant

    // Defining a static method
    public static function Courses(param:String):String
    {
      var re:String;          // The variable that will be returned

      // define "switch" instruction to set different posible values for "re"
      switch(param)
      {
        case 'web':
          re = elStatic.prop2;        // "re" receives the value of the static property "prop2"
          break;
        case 'coursesweb':
          re = SITE;        // "re" receives the value of the static constant "SITE" ( or elStatic.SITE; )
          break;
        default: re = 'Default value';
      }

      return re;            // Returns "re" value
    }
  }
}
- Because the "Courses" method is declared as static, can use only variables and constants that are also static. If, for example, you add in it's body "re = this.prop1;" or "re = TUTORIALS;" it will generate an error.

- In the Flash document add the following code:
var obj3:elStatic = new elStatic();
trace(elStatic.Courses(obj3.TUTORIALS));        // ActionScript
// trace(obj3.Courses(elStatic.SITE));        // generate an error

// Call the static method, uses a static constant for argument
trace(elStatic.Courses(elStatic.SITE));        // coursesweb
- if you delete the backslashes (//) in front of the expression "trace(obj3.Courses(elStatic.SITE));", it will generate an error. The method "Courses()" having the "static" attribute is not recognized in the instance of the class ("obj3"), but can be accessed with the class name (elStatic.Courses('argument')).

To download the FLA and AS files with the examples of this lesson, click: Classes - Static Elements

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
}
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
Classes - Static Elements

Last accessed pages

  1. Zuma Honey (1371)
  2. SHA512 Encrypt hash in JavaScript (5059)
  3. Backgammon (1291)
  4. Strategy Games (3243)
  5. Sweet Home Decoration (45)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (47785)
  2. Read Excel file data in PHP - PhpExcelReader (35922)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (35547)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (32047)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (29323)