Daily Archives: June 25, 2009

JavaFX Init and Postinit Blocks Order

Introduction

I noticed in JavaFX there isn’t really the same kind of concept as a constructor in Java. I want to create an example that will show the order of the init block and postinit block execution when an object is instantiated. I created a super class (SuperClass) and a subclass (SubClass extends SuperClass) to see the order of init blocks and postinit blocks executed.

This is the order the init and post init block run execution:

  1. Init block executes right after the attribute values are set or initialized.
  2. Postinit block executes right after the object is finished initialized.

Example

package initstuff;

/**
 * This will simply display the order of init block order.
 * @author carldea
 */

var x:Integer = count();

function count():Integer {
   Main.x=Main.x+1;
};

class SuperClass{
   init {
      println("{x} SuperClass init block");
      count();
   }
   postinit{
      println("{x} SuperClass postinit block");
      count();
   }
}

class SubClass extends SuperClass{
   init {
      println("{x} SubClass init block");
      count();
   }
   postinit{
      println("{x} SubClass postinit block");
      count();
   }
}

var myJfxObject = SubClass{}

Output

1 SuperClass init block
2 SubClass init block
3 SuperClass postinit block
4 SubClass postinit block