Spec-Zone .ru
спецификации, руководства, описания, API

Instance Initialization

Initialization Order

A new JavaFX class instance is initialized in this order:

  • The object is created.
  • The Java superclass default constructor, if any, is executed.
  • The values of the object literal's instance variable initializers are computed (but not set).
  • The instance variables of JavaFX superclasses are set.
  • The instance variables of this class are set, in lexical order.
  • The init block, if any, is evaluated. The instance is now initialized.
  • The postinit block, if any, is evaluated.

The next section will cover how instance variables are set.

Instance Variable Initialization Avenues

The value of an instance variable at the end of initialization can be set in any of several ways: value provided in the object literal, the initial value specified in the variable declaration, an initializingExpression on a variable override declaration, or an assignment in an initBlock. These avenues are discussed below.

Class instances are created with object literal expression (a newExpression can be considered, for these purposes as equivalent to an object literal that sets no instance variables). For example:

var fu = Foo { x: 14 }

Here the instance variable x in Foo is set by the object literal to be 14 (note: x may have been declared in a superclass of Foo).

The declaration of an instance variable explicitly or implicitly sets a default value for the variable. For example it could be set explicitly:

class Foo {
   var x = 99;
}

Here x has an initializingExpression.

If no initializingExpression is provided, the default value for the type is the default value of the type of the instance variable -- see the Types and Values chapter. For example:

class Answers {
   var ungulate : Boolean;
}

Since false is the default value for the Boolean type, the default value of ungulate is false.

A variableOverrideDeclaration can override the default value. For example:

class Shed {
   var siding = "aluminum";
}

class FancyShed extends Shed {
   override var siding = "copper";
}

Here FancyShed overrides the default value of siding. Note that a variableOverrideDeclaration that does not have an initializingExpression will not override the default value.

Exactly one of the above will set the initial value. If the value is provided in the object literal, that will be used. Otherwise, if an override for the variable provides an initializingExpression it will be used. If it is not overridden, an explicit initializingExpression will supply the initial value. Failing all that, the default value for the type will be used.

After one of the above has set the instance variable, the initBlock, if present, is executed. This block can reset the instance variable.