Now that you have a classpath folder set, let's start off by just writing the most simple class file there could ever be. It will simply trace a message to the screen. Mainly, this will be a test to make sure we've got communication with our classes, but also we'll also refine the folder structure a little bit by creating subfolders beneath our "portal" folder to store our classes in. Don't worry; it will all become clear as we go along.

Inside of Flash, click the File menu, choose "New" and then "Actionscript File." In the new Actionscript document's script pane, type the following:

package com.mysite.testing {
    public class TestClass {
        public function TestClass() {
            trace("TestClass working");
        }
    }
}

Note that the folders "com," "mysite," and "testing" don't exist yet, but that's okay. We will create them in the process of saving the file. Go to File, Save As. In the Save As dialog box, first navigate to the folder you created earlier as a "portal" directory. Next, find the "New Folder" button and click it to create a new folder (alternatively, right-click somewhere in the file/folder list and choose "New" then "folder."). Call the new folder "com." Double click the new com folder to enter it. Make a new folder inside of it called "mysite" (Here you may substitute the name of your own website if you want. If not, the name "mysite" will do just fine, until you get the hang of all this). Double click the mysite folder to enter it. Create a new folder called "testing." Double click testing to enter it. Finally, save the file as TestClass.as.

Wow! That was a lot of folder creation, and hopefully it doesn't seem too confusing. But here's the deal: Now your file's location matches the location spelled out in the package declaration. Note that the name of the "portal" directory is not included in this line, because that directory is flash's starting point, and that's exactly why I like to think of it as being a portal. It's the folder that flash already knows about, because we set it in the classpath, so all we need to be concerned about is organizing everything beneath that. The folders "com" and "mysite" will be the common denominators here (we'll always use them), but will remain empty, and the folders beneath "mysite" will be named according to the functionality of the classes inside them. Since we just wrote a TestClass, we just created a package for it called "testing," and we might use the "testing" package to store all the files we create where we test new things. Or you can feel free to delete it later. I just used it for an example.

Let's put the TestClass to the test and make sure it works. Choose File, New, and create a new Flash File (Actionscript 3.0) document. Click on the first frame of the timeline, and press F9 to go to the actions panel. Type the following two lines:

import com.mysite.testing.TestClass;
var testClass:TestClass = new TestClass();

Press CTRL-Enter to test the movie. You should immediately get a message traced to the output window saying “TestClass working.” That’s because that instruction was written into the public function with the same name as the class. This special function is known as the constructor, and it runs immediately one time whenever an object is created from the class with external code using the “new” keyword. So, the constructor function is a good place to put the code that you want to have run automatically whenever a new object is created from your class. The next class we’ll write will be a good example of this.

It's also interesting to note here that since you have your own global classpath folder set, it's not even necessary to save the fla file anywhere! The folder that was set gives Flash an additional place to look for classes, and also an additional place to look when we tell it to import something.