The Class Constructor (.NET) A class constructor executes before a class is referenced or used. The class constructor must be declared as strict
private, and there can be at most one class constructor declared in a class. Descendants can declare their own class
constructor, however, do not call inherited within the body of a class constructor. In fact, you cannot call a class
constructor directly, or access it in any way (such as taking its address). The compiler generates code to call class
constructors for you.
There can be no guarantees on when a class constructor will execute, except to say that it will execute at some time
before the class is used. On the .NET platform in order for a class to be "used", it must reside in code that is actually
executed. For example, if a class is first referenced in an if statement, and the test of the if statement is never true
during the course of execution, then the class will never be loaded and JIT compiled. Hence, in this case the class
constructor would not be called.
The following class declaration demonstrates the syntax of class properties and fields, as well as class static methods
and class constructors:
type
TMyClass = class
strict protected
// Accessors for class properties must be declared class static.
class function GetX: Integer; static;
class procedure SetX(val: Integer); static;
public
class property X: Integer read GetX write SetX;
class procedure StatProc(s: String); static;
strict private
class var
FX: Integer;
class constructor Create;
end;
147