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

Assigning One Variable's Value to Another

When we last saw our virtual zoo program, we had just finished creating a constructor method for the VirtualPet class. The constructor method defined a single parameter, name, whose value was supplied externally by object-creation code in the VirtualZoo class. Here's the code for the VirtualPet and VirtualZoo classes, as we left them:

// VirtualPet class
package zoo {
  internal class VirtualPet {
    internal var petName = "Unnamed Pet";

    public function VirtualPet (name) {
    }
  }
}

// VirtualZoo class
package zoo {
  public class VirtualZoo {
    public function VirtualZoo () {
      var pet = new VirtualPet("Stan");
    }
  }
}

Now that we know how to use variables in expressions, we can use the name parameter to assign the value "Stan" to the new VirtualPet object's petName instance variable.

Recall that to assign an instance variable a new value, we use the following generalized code:

object.instanceVariable = value

According to that generalized code, we need to start our variable assignment by referring to an object. In this case, that object is the new VirtualPet instance being created. To refer to it, we use the keyword this, which is an automatically created parameter whose value is the object being created:

this

Tip

Within the body of a constructor method, the object being created is known as the current object. To refer to the current object, we use the keyword this.

After the keyword this, we write a dot, followed by the name of the instance variable whose value we wish to assign—in this case petName.

this.petName

Finally, we write an equals sign, then the value we wish to assign to the instance variable:

this.petName = value

The value we wish to assign is the value associated with the name parameter. Hence, for value, we write simply: name.

this.petName = name

At runtime, ActionScript replaces name, in the preceding code, with the value passed to the VirtualPet constructor. That value is then assigned to the instance variable petName.

Here's the assignment code as it appears in our VirtualPet constructor:

package zoo {
  internal class VirtualPet {
    internal var petName = "Unnamed Pet";

    public function VirtualPet (name) {
      this.petName = name;
    }
  }
}

Now that petName's value is assigned in the VirtualPet constructor, we can remove the redundant initial value "Unnamed Pet" in the petName variable definition. The petName variable definition used to look like this:

internal var petName = "Unnamed Pet";

From now on, it will look like this (notice the removal of the variable initializer):

package zoo {
  internal class VirtualPet {
    internal var petName;

    public function VirtualPet (name) {
      this.petName = name;
    }
  }
}

Tip

An expression that assigns a variable a value, such as this.petName = name is known as an assignment expression. The equals sign in assignment expressions is an operator called the assignment operator.

Copies and References

In the preceding section, we learned how to assign one variable's value to another. Specifically, we assigned the value of the parameter name to the instance variable petName. Here's the code:

this.petName = name;

The result of assigning the one variable's value to another variable depends on the type of value being assigned.

In an assignment where the source variable's value is an instance of String, Boolean, Number, int, or uint, ActionScript makes a copy of that value and assigns the copy to the destination variable. After the assignment, two separate copies of the original value exist in system memory—the original value itself, and the copy of that value. The source variable points, or refers, to the original value in memory. The destination variable refers to the new value in memory.

By contrast, in an assignment where the source variable's value is an instance of a custom class or an instance of a built-in class other than String, Boolean, Number, int, or uint, ActionScript associates the second variable directly with the first variable's value. After the assignment, only one copy of the value exists in memory, and both variables refer to it. The variables are said to share a reference to the single object in memory. As a natural consequence, changes to the object made through the first variable are reflected by the second variable. For example, consider the following code, which creates two local variables, a and b, and then assigns a's value to b:

var a = new VirtualPet("Stan");
var b = a;

When the first line of the preceding code runs, ActionScript creates a new VirtualPet object, stores that object in memory, and then associates the local variable a with that object. When the second line of the preceding code runs, ActionScript associates the local variable b with the VirtualPet object already referred to by a. Changes made to the VirtualPet object through a are, hence, naturally reflected by b, and vice versa. For example, if we assign petName using the code b.petName = "Tom", then subsequently retrieving a.petName also yields "Tom." Or, if we assign petName using the code a.petName = "Ken", then subsequently retrieving b.petName also yields "Ken."

Tip

A variable associated with an object does not store or contain that object—it simply refers to that object. The object, itself, is stored internally by ActionScript, in system memory.

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