Adam Flater » MixIn http://www.adamflater.net Tech, UX, Design Fri, 13 Dec 2013 05:00:11 +0000 en-US hourly 1 http://wordpress.org/?v=3.5.1 Static Code Blocks in ActionScript! http://www.adamflater.net/2007/03/02/static-code-blocks/ http://www.adamflater.net/2007/03/02/static-code-blocks/#comments Fri, 02 Mar 2007 19:26:00 +0000 adamflater http://www.adamflater.net/?p=5 shot_1If you’re coming to the Flex world as a java user one thing you might miss is static code blocks. Until today I didn’t think this was possible in Flex/AS 3, but after a conversation with a coworker (Jim : psalterego.com) I found out static code blocks are possible.

First off, let me give a quick example of a static code block in java:

public class StaticCodeBlockExample {
    static {
        // my static code
    }
    ...
}

The code inside the static block is run when StaticCodeBlockExample is loaded. Among other tasks, this concept is pretty handy for initializing any static properties that might depend on utility classes to setup.

The AS 3 equivalent:

package {

import mx.managers.ISystemManager;

[Mixin]
public class StaticCodeBlockExample {
    public static function init(systemManager:ISystemManager) : void {
        trace("my static code block");
    }
}
}

As long as this class is referenced somewhere in your application, the init method gets called when the application is starting up. Essentially, we have a static code block!

]]>
http://www.adamflater.net/2007/03/02/static-code-blocks/feed/ 4