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

Constructor Methods

A constructor method (or, constructor, for short) is a discrete set of instructions used to initialize the instances of a class. To create a constructor method, we use a function definition within a class block, as shown in the following generalized code:

class SomeClass {
  function SomeClass () {
  }
}

In the preceding code, the keyword function begins the constructor method. Next comes the constructor method name, which must exactly match the class name (case sensitivity matters!). The constructor method name is followed by a pair of parentheses that contain a list of constructor parameters, which we'll study later. The curly braces ({}) following the parameter list are a block statement, just like the block statements in package and class definitions. A constructor method's block statement is known as the constructor body. The constructor body contains the directives that initialize instances. Whenever a new instance of SomeClass is created, the directives in the constructor body are executed (sequentially, from top to bottom). Executing the directives in the constructor body is known as executing the constructor or, more casually, running the constructor.

Tip

Constructor methods are created using the function keyword because they are, technically speaking, a type of function. We'll study functions in Chapter 5.

When a class does not define a constructor function explicitly, ActionScript automatically provides a default constructor that performs no initialization on new instances of the class. Despite this convenience, as a best practice, always include a constructor, even if it is just an empty one. The empty constructor serves as a formal indication that the class design does not require a constructor and should be accompanied by a comment to that effect. For example:

class SomeClass {
  // Empty constructor. This class does not require initialization.
  function SomeClass () {
  }
}

Unlike classes, the accessibility of constructor methods cannot be controlled with access-control modifiers. In ActionScript 3.0, all constructor methods are implicitly considered public. (Future versions of ActionScript might, however, allow for non-public constructor methods.) As a matter of style, this book always includes the public access-control modifier when defining constructor methods, stressing the fact that all constructor methods must be public. The following code demonstrates:

class SomeClass {
  public function SomeClass () {
  }
}

Tip

The rule that constructor methods must be public in ActionScript 3.0 was instituted due to engineering time constraints and volatility of the ECMAScript 4 Language Specification. For details, see Sho Kuwamoto's article at: http://kuwamoto.org/2006/04/05/as3-on-the-lack-of-private-and-protected-constructors. (Sho is Adobe's Flex Builder 2's development team lead.)

The constructor method of an application's main class plays a special role in a program. It provides an opportunity to execute code immediately after the application has started. As such, the constructor method of an application's main class is considered the program point of entry.

The following code adds a constructor method to our VirtualZoo class (shown in bold):

package zoo {
  public class VirtualZoo {
    public function VirtualZoo () {
    }
  }
}

Our application now has an official point of entry. When our application starts, the Flash runtime will automatically create a VirtualZoo instance, executing the VirtualZoo constructor method in the process. Given that our application is a virtual zoo, the first thing we'll do in the VirtualZoo constructor method is create a VirtualPet object (i.e., add a pet to the zoo). We'll learn how to create objects next.

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