instance()

instance(class : 4D.Class) : Object

Together with registerSingleton(), this method is used to manage singletons, which are single, global instances of a class.

If a class has a constructor with no parameters, you can get a singleton instance of the class by calling instance(). If the singleton instance has not yet been created, it is instantiated and registered with registerSingleton(). The singleton instance is returned by this method.

NOTE

See registerSingleton() for important considerations when defining a singleton class.

Example

// Greeter
Class constructor()
  This.greeting:="Hello"

Function greet($name: Text) : Text
  return _.format("${1} ${2}!"; This.greeting; $name)

// greetUser
ALERT(instance(cs.Greeter).greet("Laurent"))
// => Hello Laurent!
Last Updated:
Contributors: Aparajita