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

An Instance Variable for Our Pet

Earlier, we learned that a local variable expires when the method or function in which it is defined finishes executing. To make sure that the VirtualPet instance in our VirtualZoo class will be accessible after the VirtualZoo constructor finishes, let's update the VirtualZoo class. Instead of assigning our VirtualPet object to a local variable, we'll assign it to an instance variable, pet. We'll make pet private so that it can be accessed by code in the VirtualZoo class only. Here's the code (the new instance variable is shown in bold):

package zoo {
  public class VirtualZoo {
    private var pet;

    public function VirtualZoo () {
      this.pet = new VirtualPet("Stan");
    }
  }
}

Over the preceding several sections, we've learned how to use instance variables to give characteristics to the objects of a class. Now let's explore how to use instance methods to give behaviors to the objects of a class.

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