Defining getters and setters

Getters and setters provide visibility to component properties and control access to those properties by other objects.

The convention for defining getter and setter methods is to precede the method name with get or set, followed by a space and the property name. It's a good idea to use initial capital letters for each word that follows the get or set.

The variable that stores the property's value cannot have the same name as the getter or setter. By convention, precede the name of the getter and setter variables with two underscores.

The following example shows the declaration of initialColor, and getter and setter methods that get and set the value of this property:

...
public var __initialColor:Color = 42;
...
public function get initialColor():Number {
  return __initialColor;
}
public function set initialColor(newColor:Number) {
  __initialColor = newColor;
}

Getters and setters are commonly used in conjunction with metadata keywords to define properties that are visible, are bindable, and have other properties. For more information, see Component metadata.