Skip to content

Runtime ~ Lazy Class Initialization

Frank Wienberg edited this page Mar 14, 2014 · 1 revision

Lazy Class Initialization

Jangaroo classes have dependencies on other classes: they may inherit from some class, implement interfaces, instantiate other classes or access static members of other classes. Jangaroo allows classes to have static initializers and static code blocks. In order to determine the sequence in which such static code is to be executed, Jangaroo provides a feature called "lazy class initialization".

The code generated by the Jangaroo compiler only "prepares" classes, i.e. tells the runtime to store them in its internal class repository. When all classes are prepared, the runtime can initialize a class by first initializing

  1. its super class,
  2. all implemented interfaces, and
  3. all imported classes for which at least one static field is accessed by this class.

Since the runtime cannot determine the last case, the compiler generates a static initializer that initializes all such dependent classes.

Finally, a class is actually initialized, when

  1. a static method of the class is called,
  2. the constructor of the class is called or
  3. joo.classLoader.init() is called explicitly (see [Calling-Jangaroo-from-JavaScript][]).

So from within Jangaroo code, under normal circumstances it is not possible to use a class before it has been initialized---in fact, you need not take care of class initialization. The only exception is when you use other classes by "reflection", i.e. determine which class to use at runtime.

Clone this wiki locally