1. Computing

Understanding Class Methods

By

In Delphi, a method is a procedure or function that performs an operation on an object. A class method is a method that operates on a class reference instead of an object reference.

If you read between the lines, you will find that class methods are accessible even when you haven't created an instance of the class (the object).

Class Methods vs. Object Methods

Every time you create a Delphi component dynamically, you use a class method: the Constructor.

The Create constructor is a class method, as opposed to virtually all other methods you'll encounter in Delphi programming, which are object methods. A class method is a method of the class, and appropriately enough, an object method is a method that can be called by an instance of the class. This is best illustrated by an example, with classes and objects highlighted in red for clarity:

myCheckbox := TCheckbox.Create(nil) ;

Here, the call to Create is preceded by the class name and a period ("TCheckbox."). It's a method of the class, commonly known as a constructor. This is the mechanism by which instances of a class are created. The result is an instance of the TCheckbox class. These instances are called objects. Contrast the previous line of code with the following:

myCheckbox.Repaint;

Here, the Repaint method of the TCheckbox object (inherited from TWinControl) is called. The call to Repaint is preceded by the object variable and a period ("myCheckbox.").

Class methods can be called without an instance of the class (e.g., "TCheckbox.Create"). Class methods can also be called directly from an object (e.g., "myCheckbox.ClassName"). However object methods can only be called by an instance of a class (e.g., "myCheckbox.Repaint").

Behind the scenes, the Create constructor is allocating memory for the object (and performing any additional initialization as specified by TCheckbox or its ancestors).

Experimenting with your own class methods

Think of AboutBox (a custom "About This Application" form). In my code, I am using something like:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TfrMain.mnuInfoClick(Sender: TObject) ;
begin
  AboutBox:=TAboutBox.Create(nil) ;
  try
   AboutBox.ShowModal;
  finally
   AboutBox.Release;
  end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

This, of course, is a very nice way to do the job, but just to make the code easier to read (and to manage), it would be much more efficient to change it to:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TfrMain.mnuInfoClick(Sender: TObject) ;
begin
  TAboutBox.ShowYourself;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

The above line calls the "ShowYourself" class method of the TAboutBox class. The "ShowYouself" must be marked with the keyword "class":

~~~~~~~~~~~~~~~~~~~~~~~~~
class procedure TAboutBox.ShowYourself;
begin
   AboutBox:= TAboutBox.Create(nil) ;
   try
    AboutBox.ShowModal;
   finally
    AboutBox.Release;
   end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Notice:

  1. The definition of a class method must include the reserved word class before the procedure or function keyword that starts the definition.
  2. AboutBox form is not auto-created (Project-Options).
  3. Put AboutBox unit to the uses clause of the main form.
  4. Don't forget to declare the procedure in the interface (public) part of the AboutBox unit.

How about class properties? While (Win32) Delphi enables you to create class (static) methods (function or procedure), you cannot mark a property of a class to be a class (static) property. False. You can! See how to simulate class properties using typed constants.

©2014 About.com. All rights reserved.