O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Class Objects

Earlier we learned that each static method and static variable is accessed through the class that defines it. For example, to access the static variable maxCalories, which is defined by the VirtualPet class, we use the following code:

VirtualPet.maxCalories

In the preceding code, the use of the class name VirtualPet is not merely a matter of syntax; VirtualPet actually refers to an object that defines the variable maxCalories. The object referenced by VirtualPet is an automatically created instance of the built-in Class class.

Every class in ActionScript is represented at runtime by an instance of the Class class. From a programmer's perspective, Class objects are used primarily to access the static variables and static methods of a class. However, like other objects, Class objects are values that can be assigned to variables, and passed to or returned from methods and functions. For example, the following revised version of our VirtualZoo class assigns the Class object representing the VirtualPet class to a variable, vp, and then uses that variable to create a VirtualPet object:

package zoo {
  public class VirtualZoo {
    private var pet;

    public function VirtualZoo () {
      var vp = VirtualPet;
      pet = new vp("Stan");
    }
  }
}

The preceding technique is used when one .swf file wishes to access another .swf file's classes, and when embedding external assets (such as images or fonts) in a .swf file. We'll study both of those scenarios in Part II of this book.

We've now finished our study of static variables and static methods. Before we move on to the next chapter, let's compare some of the terms we've learned with those used in C++ and Java.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required