flash oop_static_attribute

Use of the static attribute

The purpose of the static property allows you to make a member usable in a class context and not of an instance. In other words, when a method or property is defined with the static attribute it can only be called from the class constructor.

Static properties are different to instance method properties which can only be called on class instances. The merit of a static property is to be global to the class, if we define a static nSpeed property in a vessel class, when modified all of the vessels will have their speed modified.

Amongst the static methods the most well known are the following:

  • Math.abs
  • Math.round
  • Math.floor
  • ExternalInterface.call

We often use these static properties in classes such as Math or System:

  • Math.PI
  • System.totalMemory
  • Security.sandboxType

Here is an example of the use of a static property. In order to understand how class instances are created we define an i static property in the Player class.

On each instantiation we increment this property and allocate the value of the id occurrence property:

package
{
public class Player
{
// definition of class properties
private var surname:String;
private var name:String;
private var age:Number;
private var town:String;
private var id:int;
// static property
private static var i:int = 0;
// id property
private var id:int;
// constructor function
function Player ( pName:String, pSurname:String, pAge:int, pTown:String
)
{
name = pName;
surname = pSurname;
age = pAge;
town = pTown;
// as each player object is created, we increment
// the i static property
id = i++;
}
// rest of the code not shown
}
}

On each creation of the player object, the constructor is triggered increments the i static property allocated by the id property occurrence.

To be able to get the number of players created we create a static method which returns the value of the i static property:

// returns the number of players created
public static function getNumberPlayers ( ):int
{
return i;
}

In order to target a static property in a class we always specify the class constructor before targeting the property:

// constructor function
function Player ( pName:String, pSurname:String, pAge:int, pTown:String )
{
name = pName;
surname = pSurname;
age = pAge;
town = pTown;
id = Player.i++;
}
// returns the number of players created
public static function getNumberPlayers ( ):int
{
return Player.i;
}

By specifying the constructor before the property a developer will be warned of the static type of property.

Here is the final code of the Player class:

package
{
public class Player
{
// constructor function
function Player ( pName:String, pSurname:String, pAge:int, pTown:String
)
{
name = pName;
surname = pSurname;
age = pAge;
town = pTown;
id = Player.i++;
}
// returns the number of players created
public static function getNumberPlayers ( ):int
{
return Player.i;
}
// recuperates the name of the player
public function getName ( ):String
{
return name;
}
// recuperates the surname of the player
public function getSurname ( ):String
{
return surname;
}
// recuperates the age of the player
public function getAge ( ):int
{
return age;
}
// recuperates the town of the player
public function getTown ( ):String
{
return town;
}
// recuperates the town of the player
public function setTown ( pTown:String ):void
{
town = pTown.charAt(0).toUpperCase()+pTown.substr ( 1
).toLowerCase();
}
// permits the change of the player surname
public function setSurname ( pSurname:String ):void
{
if ( pSurname.length <= 30 ) surname = pSurname;
else trace ("The surname specified is too long");
}
// allows the change of the player name
public function setName ( pName:String ):void
{
if ( pName.length <= 30 ) name = pName;
else trace ("The name specified is too long");
}
// method allowing the player to be presented
public function present ( ):void
{
trace("My name is " + name + ", I am " + age + " years old." );
}
// displays a description of the Player object
public function toString ( ):String
{
return "[Player name : " + name +", surname : " + surname + ", age :
" + age + ", town : " + town + "]";
}
}
}

In order to know how many players have been created we call the getNumberPlayers method directly from the player class:

// instancing of a player
var myPlayer:Player = new Player("Stevie", "Wonder", 57, "Michigan");
// recuperation of surname
// displays : 1
trace( Player.getNumberPlayers() );
// instancing of a player
var mySecondPlayer:Player = new Player("Bobby", "Womack", 57, "Detroit");
// recuperation of surname
// displays : 2
trace( Player.getNumberPlayers() );

If we try to call a class method on an instance of the class:

// creation of known player :)
var myPlayer:Player = new Player ("Stevie", "Wonder", 57, "Michigan");
// call of a static method on a class instance
myPlayer.getNumberPlayers();

The compiler returns the following error:

1061: Call to a possibly undefined method getNumberPlayers through a reference with static type Player.

Contrary to class property occurrences, the class properties can be directly initialised after their definition. This is because a static method only exists in classes and not in instances, so if we create ten players the constructor of the player class will be triggered ten times, but the initialisation of the class and of the static properties will happen only once. Also, our playerArray is not recreated on each triggering of the constructor.

It is impossible to use the this keyword in the class method, only instance methods allow it. A static method evolves in the class context and not in an instance. If we try to reference this in the static method, compilation is possible:

// returns the number of players created
public static function getNumberPlayers ( ):int
{
return this.i;
}

The following error message is displayed:

1042: The this keyword can not be used in static methods. 
It can only be used in instance methods, function closures, and global code.

On the other hand, the instance methods can target a property or static method. The constructor of the player class increments the i static property:

// constructor function
function Player ( pName:String, pSurname:String, pAge:int, pTown:String
)
{
name = pName;
surname = pSurname;
age = pAge;
town = pTown;
// As each player object is created, we increment the I static property
id = Player.i++;
}

When we need to define a property or method on a global scope available to all class instances we use the static property attribute. Certain classes only contain static methods, we can imagine a checkForm class having different methods allowing the validation of a form.

// direct verification of the email using a static method
var mailValid:Boolean = FormTools.checkEmail( "bobby@groove.com" );

This technique allows you to create utility classes, making access to functionalities without specific object instancing. We dip directly into the functionality class that interests us.

Worth remembering

  • The use of the this keyword is forbidden in the static methods
  • On the other hand, an instance method of the class can target or call a static method or class property.
  • Methods or static properties have a global scope within the class.







Adobe Training center


Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42