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 to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Classes - Static Elements

Last accessed pages

  1. Shape Tween - Flash Animation (6149)
  2. The Mastery of Love (7440)
  3. Get Mime Type of file or string content in PHP (6230)
  4. Countdown Timer with starting time added into a form (11533)
  5. Disable button and Enable it after specified time (17533)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)