Importing classes

To reference a class in another script, you must prefix the class name with the class's package path. The combination of a class's name and its package path is the class's fully qualified class name. If a class resides in a top-level classpath directory—not in a subdirectory in the classpath directory—then its fully qualified class name is just its class name.

To specify package paths, use dot notation to separate package directory names. Package paths are hierarchical, where each dot represents a nested directory. For example, suppose you create a class named Data that resides in a com/network/ package in your classpath. To create an instance of that class, you could specify the fully qualified class name, as follows:

var dataInstance = new com.network.Data();

You can use the fully qualified class name to type your variables, as well:

var dataInstance:com.network.Data = new Data();

You can use the import statement to import packages into a script, which lets you use a class's abbreviated name rather than its fully qualified name. You can also use the wildcard character (*) to import all the classes in a package.

For example, suppose you created a class named UserClass that's included in the package directory path macr/util/users:

// In the file macr/util/users/UserClass.as
class macr.util.users.UserClass { ... }

Suppose that in another script, you imported that class as follows using the import statement:

import macr.util.users.UserClass;

Later in the same script you could reference that class by its abbreviated name:

var myUser:UserClass = new UserClass();

You can use the wildcard character (*) to import all the classes in a given package. For example, suppose you have a package named macr.util that contains two ActionScript class files, foo.as and bar.as. In another script, you could import both classes in that package using the wildcard character, as shown below.

import macr.util.*;

In the same script, you can then reference either the foo or bar class directly.

var myFoo:foo = new foo();
var myBar:bar = new bar();

The import statement applies only to the current script (frame or object) in which it's called. If an imported class is not used in a script, the class is not included in the resulting SWF file's bytecode, and the class isn't available to any SWF files that the FLA file containing the import statement might call. For more information, see import.