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

Advertisements

One thought on “JavaFX Init and Postinit Blocks Order

  1. Pingback: Java desktop links of the week, June 29 | Jonathan Giles

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s