Importing classes and packages

To reference a class in another script, you must prefix the class name with the class's package name. 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 class name is also its fully qualified class name.

To specify package paths, use dot (.) notation to separate package directory names. Package paths are hierarchical; that is, each dot represents a nested directory. For example, suppose you create a class named ClassName that resides in a com/adobe/docs/learnAs2 package in your classpath. To create an instance of that class, you could specify the fully qualified class name.

You can also use the fully qualified class name to type your variables, as shown in the following example:

var myInstance:com.adobe.docs.learnAs2.ClassName = new com.adobe.docs.learnAs2.ClassName();

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. If you use the wildcard character, you don't need to use the fully qualified class name each time you use the class.

For example, suppose that in a script you imported the above class using the import statement, as shown in the following example:

import com.adobe.docs.learnAs2.util.UserClass;

Later, in the same script, you could reference that class by its abbreviated name, as shown in the following example:

var myUser:UserClass = new UserClass();

You can use the wildcard character (*) to import all the classes in a given package. Suppose you have a package named com.adobe.utils that contains two ActionScript class files, ClassA.as and ClassB.as. In another script, you could import both classes in that package using the wildcard character, as shown in the following code:

import com.adobe.utils.*;

The following example shows that you can then reference either of the classes directly in the same script:

var myA:ClassA = new ClassA();
var myB:ClassB = new ClassB();

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 load.

NOTE

The following exercise is part of Example: Using custom class files in Flash which continues the examples Example: Writing custom classes. If you need ClassA and ClassB, you can download the class files from www.helpexamples.com/flash/learnas/classes/.

To import a class or package:

  1. Open the file called package_test.fla.
  2. Type the following code into the Script window:
    import com.adobe.utils.*;
    var a = new ClassA(); // ClassA constructor
    var b = new ClassB(); // ClassB constructor
    

    The previous block of code begins by importing each of the classes within the com.adobe.utils package by using the wildcard (*) character. Next, you create a new instance of the ClassA class, which causes the constructor method to trace a message to the Output panel. An instance of the ClassB class is also created, which sends debugging messages to the Output panel.

  3. Save your changes to the Flash document before you proceed.

To continue using these class files in a Flash file, see Creating instances of classes in an example.