One of the most popular posts on my blog adamflater.net has been Static Code Blocks!. Due to the amount of requests for that post I thought it made for a good topic to feature on InsideRIA regarding ActionScript and static code.
First off, what is static code? Static code is executable code that is said to belong to a Class. An instance method of an Object exists N times for the N instances of the Class type of Object, but static code exists only once no matter how many instances of the Object exist. The other difference between an instance method and static code is that static code has a static scope. In other words, the code executing in the static block may only access the other static members of the Class that it belongs to.
There are essentially three ways to create static code blocks in ActionScript: Using the Mixin Class meta tag, wrapping the code in an unnamed block, and using a static method initializer pattern. In reviewing each of these solutions it's important to consider order of execution and scope.
So, let's take a look at these three solutions in the order that they're executed.
1. Using a static var as an initializer
This solution is executed by the Flash Player first out of the other two solutions. It involves using a static variable to call a static method.
Generally it looks like this:
private static var initialized : Boolean = initializer();
private static function initializer() : Boolean
trace( "static method init'er" );
In this example initialized is a static variable that technically belongs to the Class (not the Object). Although this variable is accessible by the Object it "lives" at the Class level. When the class is being loaded by the Flash Player the variable initialized is created and assigned the value returned by the initializer method. Like the other examples, this code is executed once no matter how many times the Class it belongs to is instantiated.
2. Unnamed code block
If, for some reason, you're using the static initializer pattern shown in the first example and you also have an unnamed code block in your class, the unnamed code block will be executed second. A simpler example is:
trace( "this is a static block" );
Again, the scope of this block is the static scope (ie Class members are accessible).
3. The Mixin meta tag
The last to execute in this series of static code blocks is the Mixin meta tag. An example of this solution is:
public class StaticBlocks
public static function init( systemManager : ISystemManager ) : void
trace( "mix-in init method" );
In this solution the init method also has a static scope limited to the Class it belongs to, but in addition there is a reference passed into the method to the application's SystemManager called systemManager. The system manager provides access to many interesting areas of your application. SystemManager is also available via the static reference: Application.application.systemManager however, in the previous two examples the classes needed to obtain a reference the SystemManager using Application.application.systemManager have not yet been created. By using the Mixin tag you're guaranteed that the Flash Player / Flex Framework has initialized and created the Application and SystemManger classes.
It's equally important to note that the other two solutions executed before the Application and SystemManager have finished initializing. This is important if you have some code that you need to have executed before these classes finish their instantiations.
Aside from initializing custom styles in components you may not find a lot of uses for static code blocks in a typical Flex application, but these techniques can come in handy if you're building a service API or Framework in Flex.
I hope this was a valuable follow up post to my original effort outline static code blocks. I'd love to hear your questions and comments.