Search code examples
actionscript-3flashavm2

AS3: Classloading order


I had a quick question on static blocks in AS3.

I have a library that requires initialization statically before any application logic is executed. If I insert a static code block like the following, will this truly be executed before everything else? (ie: is it safe to assume that everything will be setup before the application starts?)

package {

    import com.tkassembled.library.MyStaticLibrary;
    import com.tkassembled.library.MyWorker;

    import flash.display.Sprite;

    public class Application extends Sprite {

        // begin static code 
        /* initialize */ {
            MyStaticLibrary.worker = new MyWorker();
        }

        public function Application() {

        }
    }
}

I would assume that the above code would execute in the following fashion:

  1. Load Application class, as it is the 'main executable'.
  2. Load MyStaticLibrary and MyWorker, executing any static blocks in them.
  3. Execute the static blocks within Application.
  4. Call the constructor and get things going.

Does anyone know if this is true or not? I guess I'll build an application to test it all out in the meantime :)


Solution

  • Did the homework, here is the execution order:

    [16] Application static block executed.
    [16] MyLibrary static blocks invoked. 
    [16] MyWorker static blocks
    [16] MyWorker constructor called. 
    [16] MyLibrary.worker set.
    [17] Application constructor executed.
    

    It really helps to understand how things work :)

    You can get my experiment files here: http://bit.ly/aKwqp6