Flash CS4 Professional ActionScript 2.0

About public, private, and static methods and properties (members)

When you write ActionScript class files in an external script file, there are four types of methods and properties that you can create: public methods and properties, private methods and properties, public static methods and properties, and private static methods and properties. These methods and properties define how Flash can access variables, and they allow you to specify what parts of your code can access certain methods or properties.

When you are building class-based applications, whether the application is small or large, it is especially important to consider whether a method or property should be private or public. Considering this ensures that your code is as secure as possible. For example, if you were building a User class, you might not want to allow people using the class to be able to change a user's ID. By setting the class property (sometimes referred to as an instance member) to private, you can limit access to the property to code within the class or subclasses of that class, meaning that no users can change that property directly.

Public methods and properties

The public keyword specifies that a variable or function is available to any caller. Because variables and functions are public by default, the this keyword is used primarily for stylistic and readability benefits, indicating that the variable exists in the current scope. For example, you might want to use the this keyword for consistency in a block of code that also contains private or static variables. The this keyword can be used with either the public or private keyword.

The following Sample class already has a public method named myMethod():

class Sample {
    private var ID:Number;
    public function myMethod():Void {
        this.ID = 15;
        trace(this.ID); // 15
        trace("myMethod");
    }
}

If you want to add a public property, you use the word "public" instead of "private," as you can see in the following sample code:

class Sample {
    private var ID:Number;
    public var email:String;
    public function myMethod():Void {
        trace("myMethod");
    }
}

Because the email property is public, you can change it within the Sample class, or directly within a FLA.

Private methods and properties

The private keyword specifies that a variable or function is available only to the class that declares or defines it or to subclasses of that class. By default, a variable or function is public, and available to any caller. Use the this keyword if you want to restrict access to a variable or function, as you can see in the following example:

class Sample {
    private var ID:Number;
    public function myMethod():Void {
        this.ID = 15;
        trace(this.ID); // 15
        trace("myMethod");
    }
}

If you want to add a private property to the previous class, you simply use the keyword private before the var keyword.

If you attempt to access the private ID property from outside the Sample class, you get a compiler error and a message in the Output panel. The message indicates that the member is private and cannot be accessed.

Static methods and properties

The static keyword specifies that a variable or function is created only once per class rather than in every object based on that class. You can access a static class member without creating an instance of the class. Static methods and properties can be set in either the public or private scope.

Static members, also called class members, are assigned to the class, not to any instance of the class. To invoke a class method or access a class property, you reference the class name, rather than a specific instance of the class, as shown in the following code:

trace(Math.PI / 8); // 0.392699081698724

If you type this single line of code in the script pane of the Actions panel, you see a result trace in the Output panel.

For example, in the previous Sample class example, you could create a static variable to keep track of how many instances of the class have been created, as demonstrated in the following code:

class Sample {
    public static var count:Number = 0;
    private var ID:Number;
    public var email:String;
    public function Sample() {
        Sample.count++;
        trace("count updated: " + Sample.count);
    }
    public function myMethod():Void {
        trace("myMethod");
    }
}

Every time you create a new instance of the Sample class, the constructor method traces the total number of Sample class instances that have been defined so far.

Some of the top-level ActionScript classes have class members (or static members), as you saw earlier in this section when you called the Math.PI property. Class members (properties and methods) are accessed or invoked on the class name, not on an instance of the class. Therefore, you don't create an instance of the class to use those properties and methods.

For example, the top-level Math class consists only of static methods and properties. To call any of its methods, you don't create an instance of the Math class. Instead, you simply call the methods on the Math class itself. The following code calls the sqrt() method of the Math class:

var squareRoot:Number = Math.sqrt(4);
trace(squareRoot); // 2

The following code invokes the max() method of the Math class, which determines the larger of two numbers:

var largerNumber:Number = Math.max(10, 20);
trace(largerNumber); // 20

For more information on creating class members, see About class members and Using class members.

For samples that demonstrates how to create a dynamic menu with XML data and a custom class file, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. The sample calls the ActionScript XmlMenu() constructor and passes it two parameters: the path to the XML menu file and a reference to the current timeline. Download and decompress the Samples zip file and navigate to the ActionScript2.0/XML_Menu folder to access these samples:

  • XmlMenu.as
  • xmlmenu.fla