User Guide
118 Object-Oriented Programming in ActionScript
Static methods
Static methods, also called class methods, are methods that are declared with the static
keyword. Static methods, which are attached to a class rather than to an instance of a class, are
useful for encapsulating functionality that affects something other than the state of an
individual instance. Because static methods are attached to a class as a whole, static methods
can be accessed only through a class and not through an instance of the class.
Static methods are useful for encapsulating functionality that is not limited to affecting the
state of class instances. In other words, a method should be static if it provides functionality
that does not directly affect the value of a class instance. For example, the Date class has a
static method named
parse(), which takes a string and converts it to a number. The method
is static because it does not affect an individual instance of the class. Instead, the
parse()
method takes a string that represents a date value, parses the string, and returns a number in a
format compatible with the internal representation of a Date object. This method is not an
instance method because it does not make sense to apply the method to an instance of the
Date class.
Contrast the static
parse() method with one of the instance methods of the Date class, such
as
getMonth(). The getMonth() method is an instance method because it operates directly
on the value of an instance by retrieving a specific component, the month, of a Date instance.
Because static methods are not bound to individual instances, you cannot use the keywords
this or super within the body of a static method. Both the this reference and the super
reference have meaning only within the context of an instance method.
In contrast with some other class-based programming languages, static methods in
ActionScript 3.0 are not inherited. For more information, see “Static properties not inherited”
on page 136.
Instance methods
Instance methods are methods that are declared without the static keyword. Instance
methods, which are attached to instances of a class instead of the class as a whole, are useful
for implementing functionality that affects individual instances of a class. For example, the
Array class contains an instance method named
sort(), which operates directly on Array
instances.