Beginning ActionScript 3.0 – Attributes 2

Help-File-256x256 If you don’t use the keyword “private” to modify the properties, then you can use the properties in the subclass in most cases. That’s the conclusion we ade in last section. Now, we’re going to talk about the exception situation.

Simple example

Before any discussion, let’s write some code.

First, add some code to the Person class.

public static var staticVar:String = “Static variable”;

public static function staticMethod():void {
trace(”static method”);

Then, add the test code in the main program.

var cookInstance:Cook = new Cook();


Run the program, you’ll get the following result.


This means that static properties are not inherited by subclasses; static properties cannot be accessed through an instance of a subclass. A static property can be accessed only through the class object on which it is defined.

So, if we want to access the static properties, we can use this way.


Static attribute

Now, let’s focus on the static attribute.

The static attribute, which can be used with properties declared with the var, const, or function keywords, allows you to attach a property to the class rather than to instances of the class. Code external to the class must call static properties by using the class name instead of an instance name.

Static properties are not inherited by subclasses, but the properties are part of a subclass’s scope chain. This means that within the body of a subclass, a static variable or method can be used without referencing the class in which it was defined.

Now, let’s add more code to the Cook class to show the usage of static properties.

Here, we use a static variable cookNumber to count the current number of Cook instances. And the method getCookNumber will return the value of cookNumber. Furthermore, when you allocate a new Cook instance, the cookNumber will automatically increase by one, because of the statement in the constructor. You’ll see the discussion of constructor in the next sectionJ

The code is as below.

private static var cookNumber:int = 0;

function Cook(){

public static function getCookNumber():int{
return cookNumber;

After the addition of Cook class, let’s test it in the main program.

var cookInstanceA:Cook = new Cook();

var cookInstanceB:Cook = new Cook();

var cookInstanceC:Cook = new Cook();

Here’s the result.


As you see, when we call the constructor the first time, the cookNumber is 1; when we call the constructor the second time, the cookNumber is 2, because there’re two instances of Cook. And the third time is 3 as we expected.

Now, you see the advantage of static methods and variablesJ

Static variable are useful for storing and sharing information that applies to an entire class of objects.

Static methods are useful for encapsulating functionality that affects something other than the state of an individual instance. Because static methods are attached to a class as a whole, static methods can be accessed only through a class and not through an instance of the class.

Besides the static properties, there’re instance properties, which were declared with the var and const keywords, but without the static keyword. Instance properties are attached to class instances rather than to an entire class, are useful for storing values that are specific to an instance or for implementing functionality that affects individual instances of a class.

Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

Leave a Reply