Class methods

[next] [prev] [prev-tail] [tail] [up]

#### 6.6.4 Class methods

Class methods are identified by the keyword Class in front of the procedure or function declaration, as in the following example:

Class Function ClassName : String;

Class methods are methods that do not have an instance (i. e. Self does not point to a class instance) but which follow the scoping and inheritance rules of a class. They can be used to return information about the current class, for instance for registration or use in a class factory. Since no instance is available, no information available in instances can be used.

Class methods can be called from inside a regular method, but can also be called using a class identifier:

Var
AClass : TClass; // AClass is of type "type of class"
begin
..
if CompareText(AClass.ClassName,'TCOMPONENT')=0 then
...

But calling them from an instance is also possible:

Var
MyClass : TObject;
begin
..
if MyClass.ClassNameis('TCOMPONENT') then
...

The reverse is not possible: Inside a class method, the Self identifier points to the VMT table of the class. No fields, properties or regular methods are available inside a class method. Accessing a regular property or method will result in a compiler error.

Note that class methods can be virtual, and can be overridden.

Class methods can be used as read or write specifiers for a regular property, but naturally, this property will have the same value for all instances of the class, since there is no instance available in the class method.

[next] [prev] [prev-tail] [front] [up]