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:
- Init block executes right after the attribute values are set or initialized.
- 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
[...] Dea posted three articles on language features in JavaFX Script. The first post discusses the order in which init and postinit blocks are called, the second post discusses how to set default values in init blocks, and finally the third post [...]