Actionscript Course

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 is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
}
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert("CoursesWeb.net"); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
Classes - Static Elements

Last accessed pages

  1. Animating Armature - Pose Frames (2393)
  2. Read Excel file data in PHP - PhpExcelReader (96725)
  3. If Else conditionals, Comparative and Logical operators (4465)
  4. Selection Tools (8137)
  5. Adding data from HTML Table Rows in Form fields (9592)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (331)
  2. Read Excel file data in PHP - PhpExcelReader (126)
  3. The Four Agreements (97)
  4. PHP Unzipper - Extract Zip, Rar Archives (95)
  5. The Mastery of Love (90)
Chat
Chat or leave a message for the other users
Full screenInchide