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 used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Classes - Static Elements

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142540)
  2. JavaScript strip_tags and stripslashes (8825)
  3. Date and Time in ActionScript 3 (10086)
  4. Bind Tool and Control Points (4430)
  5. Clear Canvas Context (8116)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (543)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)