What was first: the chicken or the egg?

Here is a little experiment (that was hanging in my drafts for some months already) with static method, static code block, [Mixin] and class constructor. Can you guess correct order of trace()s dispatched by using the following Item class? Notice [Mixin] will only be understood and init() called with flex application.

package
{
    import mx.managers.SystemManager;

    [Mixin]
    public class Item
    {
        public static const C:String = publicStaticConst();
        
        trace("Item::static code block");
        
        public function Item()
        {
            trace("Item::Item()");
        }
        
        public static function init(systemManager:SystemManager):void
        {
            trace("Item::init()");
        }
        
        public static function publicStaticConst():String
        {
            trace("Item::publicStaticConst");
            return "";
        }
    }
}

… and a simple application:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               minWidth="955" minHeight="600" creationComplete="init()">
    <fx:Script>
        <![CDATA[
            public var i:Item;
            
            private function init():void
            {
                trace("### new Item ###");
                i = new Item;
            }
        ]]>
    </fx:Script>
</s:Application>

Here is the correct answer:

Item::publicStaticConst
Item::static code block
Item::init()
### new Item ###
Item::Item()

The first code ever executed in your class is a static method that is being called with static variable definition.

Where to go from here:

1 comment so far

  1. focus July 28, 2011 16:29

    Nice! Also you could look at the Jackson’s related articles about classes boot-up:
    http://jacksondunstan.com/articles/253
    http://jacksondunstan.com/articles/1038

Leave a comment

Please be polite and on topic. Your e-mail will never be published.