Importing classes and packages

If you create many utility classes or include multiple ActionScript files to get at commonly used functions, you might want to store them in a set of classes in their own package. You can import ActionScript classes and packages using the import statement. By doing this, you do not have to explicitly enter the fully qualified class names when accessing classes within ActionScript.

The following example imports the MyClass class in the MyPackage.Util package:

<mx:Script>
   import MyPackage.Util.MyClass;
   ...
</mx:Script>

In your ActionScript code, instead of referring to the class with its fully qualified package name (MyPackage.Util.MyClass), you can refer to it as MyClass, as the following example shows:

import MyPackage.Util.MyClass;
function createClass() {
tObj = new MyClass;
}

Note: All statements other than import and variable declarations must be inside functions in an <mx:Script> tag.

You can also use the wildcard character (*) to import all the classes in a given package. For example, the following statement imports all classes in the MyPackage.Util package:

import MyPackage.Util.*;

Flex searches the ActionScript classpath for imported files and packages.

If you import a class but do not use it in your application, the class is not included in the resulting SWF file's bytecode. As a result, importing an entire package with a wildcard does not create an unnecessarily large SWF file.


Version 1.5

 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/15/flex_docs_en/00000053.htm