Class syntax in Actionscript3.0

The package wrapper
With AS3 some changes have been made to the way classes are to be defined. One of the most obvious ones is that class files now have a package wrapper in them.

package com.rubenswieringa {
  public class MyClass {
    public function MyClass ():void {
    }
  }
}

Leaving behind the utter basics, I figured that, being that the class has a package wrapper, perhaps multiple classes could now be defined within one .as file. Wrong. In Flex Builder 2, attempting to declare multiple classes in one package wrapper will result in errors, regardless of their nature (internal, public, etc.).
Fortunately, in the AS3 version for the Flash9 Alpha, packages are allowed to carry multiple classes..

Interesting to note is that with AS3, you can now place statements outside of method definitions as follows:

package com.rubenswieringa {
  public class MyClass {
    trace("oi!");
    public function MyClass ():void {
    }
  }
}

These statements will be executed “when the class definition is first encountered and the associated class object is created” (from LiveDocs).

Helper classes
Outside of the package wrapper, however, multiple class declarations are allowed, called helper classes. Here’s an example:

package com.rubenswieringa {
  public class MyClass {
  }
}
class Helper1 {
}
class Helper2 {
}

Note that helper classes are only available to the main class in the same .as file (in the above example it would be ‘MyClass’). Perhaps surprising is that a subclass is also not allowed to use helper classes belonging to its superclass.

Class attributes
What struck me most about class attributes in AS3 is that, even though a class can be declared public, its opposite (private) is not allowed as a class attribute. The protected attribute isn’t allowed for class declarations either.
All in all, classes can be declared either public or internal, of which the latter is the default, meaning that when you do not specify an attribute, the compiler will assume the class is meant to be internal.

Apart from the public and internal keywords, there are two other attributes allowed for class declarations, being dynamic and final. Here, unlike the two previously discussed attributes, having one applied to a class, does not mean the other is prohibited, so a class can be both dynamic and final at the same time.
Making your class final means it cannot be extended and I think having this in Actionscript is a huge plus, since it’s a rather important aspect of OOP.

Last words..
Tons of thanks go out to Trevor McCauley for teaching me most of the logic in the matters discussed in the above..

14 thoughts on “Class syntax in Actionscript3.0

  1. I dont think a constructor is allowed a return type…as indicated in your first example. Thats prolly gonna cause someone a bit of confusion.

    package com.rubenswieringa {
    public class MyClass {
    public function MyClass ():void {
    }
    }}

  2. Hey kc, although constructors (in AS3) aren’t allowed to actually return value, they are allowed a return-type.
    I always add the “:void” part just because I (personally) think it looks a bit messy if you leave it out (every other method does have a return-type, right?)..

  3. Hi , i have a question: How to run a function from another class ?
    I’m new in As3 (verry noob :D).
    Let me explain, …so :
    first.as
    ———————————-
    package {
    import flash.display.*;
    import flash.text.*;
    public class first {
    var txt1:TextField;
    public function first () {
    text1:txt1 = new TextField ();
    text1.text = ” test text 1″;
    addChild (text1);
    if (text1.text = “test text 1″;) {
    // hir i want to execut second function from second class
    }
    }
    }
    }

    second.as
    ———————————————–
    package {
    import flash.display.*;
    import flash.text.*;
    public class second {
    var txt2:TextField;
    public function first () {
    text2:txt2 = new TextField ();
    text2.text = ” test text 2″;
    text2.x = 100;
    addChild (text2);
    }
    }
    }

  4. One more question:
    How do i put a event listener to output ?

    lets say when output say “start” , i want a event to pick it up .
    you know what i mean ?

  5. Hey Chris,

    This post is about syntax, I think you might be having some trouble with the actual concept of object-oriented design, try picking up a copy of Colin Moock’s ‘Essential ActionScript 3.0’.

Comments are closed.